Python Forum
Convert combobox user input in to date with tkinter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert combobox user input in to date with tkinter
#6
This is my code right now:
from datetime import datetime
from tkinter.ttk import *
from tkinter import *
from openpyxl import *


class Application(Tk):
    def __init__(self, *args, **kwargs):
        Tk.__init__(self, *args, **kwargs)

        d29 = list(range(1, 30))
        d30 = list(range(1, 31))
        d31 = list(range(1, 32))

        self.months = dict(
            January=d31,
            February=d29,
            March=d31,
            April=d30,
            May=d31,
            June=d30,
            July=d31,
            August=d31,
            September=d30,
            October=d31,
            November=d30,
            December=d31)

        self.lbl_birth = Label(self, text="Birthday:", font=('times', 14))
        self.lbl_birth.grid(row=1, column=1)

        self.cb_day = Combobox(self, values=self.months["January"])
        self.cb_day.grid(row=1, column=2)
        self.cb_day.set("1")

        self.cb_month = Combobox(self, values=[*self.months])
        self.cb_month.bind('<<ComboboxSelected>>', self.update)
        self.cb_month.grid(row=1, column=3)
        self.cb_month.set("January")

        self.cb_year = Combobox(self, values=list(range(1996, 2006)))
        self.cb_year.grid(row=1, column=4)
        self.cb_year.set("2000")

        day, month, year = self.cb_day.get(), self.cb_month.get(), self.cb_year.get()
        mydate = datetime.strptime(f'{day} {month} {year}', '%d %B %Y')
        today = datetime.now()
        years = today.year - mydate.year

        
    def update(self, event):
        self.cb_day["values"] = self.months[self.cb_month.get()]
        self.cb_day.set("1")


if __name__ == "__main__":
    app = Application()
    app.title('My Birthday App')
    app.mainloop()
Thank you
Reply


Messages In This Thread
RE: Convert combobox user input in to date with tkinter - by Ame - Jul-01-2020, 08:13 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGUI] [Solved]Help storing in user input from line edit Extra 2 1,789 May-12-2022, 07:46 PM
Last Post: Extra
  [PyQt] How can I sync Combobox index to other combobox index? nickzsche 2 2,422 Jan-03-2022, 12:29 PM
Last Post: Axel_Erfurt
  Convert tkinter to pyqt razs 6 5,733 Aug-29-2021, 11:08 AM
Last Post: razs
Question [Tkinter] Can I set background color for each item in tkinter Combobox? water 1 5,167 Dec-10-2020, 07:48 PM
Last Post: Larz60+
  [Tkinter] Getting Input from Tkinter Entry juliabrushett 6 21,520 May-30-2020, 03:29 PM
Last Post: Larz60+
  Create an identification code from user input PeroPuri 1 1,931 Apr-11-2020, 11:56 AM
Last Post: Larz60+
  How can a user send a message via Contact Form in tkinter karolp 0 2,399 Apr-08-2020, 08:00 PM
Last Post: karolp
  [PyQt] Python PyQt5 - Change label text dynamically based on user Input ppel123 1 13,827 Mar-20-2020, 07:21 AM
Last Post: deanhystad
  [Tkinter] Tkinter delete combobox content and not the list LagratteCchouette 4 8,542 Dec-29-2019, 11:04 AM
Last Post: LagratteCchouette
  PyQt5: How do you set the user input of a line edit to a specific variable? YoshikageKira 17 11,871 Dec-26-2019, 03:18 PM
Last Post: Denni

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020