![]() |
Convert combobox user input in to date with tkinter - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: Convert combobox user input in to date with tkinter (/thread-27968.html) |
Convert combobox user input in to date with tkinter - Ame - Jun-29-2020 Hi! I'm having a little bit of difficulty with a couple of things with my script: 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") items = [self.lbl_birth, self.cb_day, self.cb_month, self.cb_year] for t in items: self.grid_columnconfigure(t, weight=1) self.grid_rowconfigure(t, weight=1) 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()When I'm writing in excel using this code: self.bday = self.cb_day.get(), self.cb_month.get(), self.cb_year.get() self.birthday = str(self.bday) sheet.cell(row=3, column=5).value = self.birthdayI get something like this: ('1', 'January', '2000')I've tried strftime and strptime to convert self.bday in a proper date format but without success (I always get some errors). Also I'm trying to calculate the age in years with this: today_year = datetime.now().strftime('%Y') age = int(today_year) - int(self.cb_year.get()) print(age)It kinda works, but I always get back "20", as if self.cb_year.get() only gives me back the default year i put in set() and not the user input. I really can't solve this by myself so thank you all for your help! RE: Convert combobox user input in to date with tkinter - Yoriz - Jun-29-2020 from datetime import datetime day, month, year = ('1', 'January', '2000') mydate = datetime.strptime(f'{day} {month} {year}', '%d %B %Y') print(mydate) today = datetime.now() print(today) years = today.year - mydate.year print(years)
RE: Convert combobox user input in to date with tkinter - Ame - Jul-01-2020 (Jun-29-2020, 09:22 PM)Yoriz Wrote:from datetime import datetime day, month, year = ('1', 'January', '2000') mydate = datetime.strptime(f'{day} {month} {year}', '%d %B %Y') print(mydate) today = datetime.now() print(today) years = today.year - mydate.year print(years) Thanks! That worked, but I'm only getting the date based on 2000-01-01. When I try to change the month it gives me back this error: "Exception in Tkinter callback Traceback (most recent call last): File "C:\Program Files (x86)\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__ return self.func(*args) TypeError: update() takes 1 positional argument but 2 were given" How can I get the all the age and the birthday that may be inserted? Thanks again RE: Convert combobox user input in to date with tkinter - Ame - Jul-01-2020 I can't find the edit button so I apologize in advance for the double post, I've solved the traceback error, but I still only get the default date that I inputted (2000,01,01). How can I get the user info from the combobox? Thank you for your help RE: Convert combobox user input in to date with tkinter - Yoriz - Jul-01-2020 Can you show the full code you have now RE: Convert combobox user input in to date with tkinter - Ame - Jul-01-2020 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 RE: Convert combobox user input in to date with tkinter - Yoriz - Jul-01-2020 Your code is grabbing the values of the Combobox's immediately after creating them. You need to have a GUI selection made first and then possibly a button to click to submit the relevant Combobox selections. The event from clicking the button would then read the current Combobox values. I think I see now that you want the year calculated when any combobox value changes 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.bind('<<ComboboxSelected>>', self.update) 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.bind('<<ComboboxSelected>>', self.update) self.cb_year.grid(row=1, column=4) self.cb_year.set("2000") def update(self, event): 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 print(f'Years: {years}') if __name__ == "__main__": app = Application() app.title('My Birthday App') app.mainloop() RE: Convert combobox user input in to date with tkinter - Ame - Jul-01-2020 Thank you, it worked! I'm very happy: I've finish my first app thanks to you. I've only a couple of question, but they are really just about style, you've already help me a lot so don't mind me if I bother you too much. My questions are: Is there a way to have a leading zero on the day of the month? Basically add it in this part. d29 = list(range(1, 30)) d30 = list(range(1, 31)) d31 = list(range(1, 32))And also is there a way to format the date without all the zero? From this 01/01/2000 00:00:00 to this 01/01/2000. Once again, you help me a lot! Thank you RE: Convert combobox user input in to date with tkinter - Yoriz - Jul-01-2020 from datetime import datetime day, month, year = ('1', 'January', '2000') mydate = datetime.strptime(f'{day} {month} {year}', '%d %B %Y') print(mydate.strftime('%d/%m/%Y')) d29 = list(f'{num:02}' for num in range(1, 30)) print(d29)
|