How to set the calendar widget to current date - 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: How to set the calendar widget to current date (/thread-26731.html) |
How to set the calendar widget to current date - scratchmyhead - May-11-2020 I have a calendar widget in my program that I copied from google. It works fine but how do I set it to current date each time the calendar is opened? Below is the code. def example1(event): top = tk.Toplevel(root) def print_sel(): print(cal.selection_get().strftime("%m-%d-%Y")) print(now.strftime("%m-%d-%Y")) departe.delete(0, END) t = departe.get() print(t) dayse.delete(0, END) departe.insert(0, cal.selection_get().strftime("%m-%d-%Y")) t = departe.get() j = arrivale.get() g=int(t[3:5]) - int(j[3:5]) dayse.insert(0, g) cal.destroy() top.destroy() cal = Calendar(top, font="Arial 14", selectmode='day', cursor="hand1", year=2020, month=5, day=3) #Here it is set to certain date but I want current date each time it is opened. cal.pack(fill="both", expand=True) ttk.Button(top, text="ok", command=print_sel).pack() #cal.bind("<Button-1>", quit()) def example2(): top = tk.Toplevel(root) ttk.Label(top, text='Choose date').pack(padx=10, pady=10) cal = DateEntry(top, width=12, background='darkblue', foreground='white', borderwidth=2) cal.pack(padx=10, pady=10) RE: How to set the calendar widget to current date - menator01 - May-11-2020 Hope this helps import datetime today = datetime.date.today() cal = Calendar(top, font="Arial 14", selectmode='day', cursor="hand1", year=today.year, month=today.month, day=today.day) RE: How to set the calendar widget to current date - scratchmyhead - May-11-2020 I get invalid syntax error on line 28 RE: How to set the calendar widget to current date - menator01 - May-11-2020 I think this code is from one of your earlier post. I was wondering the same thing and it worked for me. #! /usr/bin/env python3.8 '''Docstring''' try: import tkinter as tk from tkinter import ttk except ImportError: import Tkinter as tk import ttk import datetime today = datetime.date.today() from tkcalendar import Calendar, DateEntry def example1(): def print_sel(): print(cal.selection_get()) top = tk.Toplevel(root) cal = Calendar(top, font="Arial 14", selectmode='day', cursor="hand1", year=today.year, month=today.month, day=today.day) cal.pack(fill="both", expand=True) ttk.Button(top, text="ok", command=print_sel).pack() def example2(): top = tk.Toplevel(root) ttk.Label(top, text='Choose date').pack(padx=10, pady=10) cal = DateEntry(top, width=12, background='darkblue', foreground='white', borderwidth=2) cal.pack(padx=10, pady=10) root = tk.Tk() s = ttk.Style(root) s.theme_use('clam') ttk.Button(root, text='Calendar', command=example1).pack(padx=10, pady=10) ttk.Button(root, text='DateEntry', command=example2).pack(padx=10, pady=10) root.mainloop() RE: How to set the calendar widget to current date - scratchmyhead - May-11-2020 You're right, my bad. I forgot to put a parenthesis in the right place. It worked. Thanks a bunch! |