如何在Python的tkcalendar中获取DateEntry的选中日期?
Hey there! Let's get that DateEntry date retrieval sorted out for you. I see two main issues in your code that are causing problems—let's break them down and fix them step by step.
First: The right way to get DateEntry's selected date
You were actually on the right track with get_date()! The problem is you were trying to call it immediately when creating the DateEntry, before the user has even had a chance to select a date. Just like your Calendar example, you need to trigger the date retrieval after the user makes their selection (like with an "OK" button).
DateEntry has two handy methods for getting the date:
get_date(): Returns adatetime.dateobject (perfect for date calculations)get(): Returns the formatted date string (matches the widget's display format)
Second: Fixing the button command bug
In your root window buttons, you wrote command=calendar_view() and command=dateentry_view()—adding those parentheses means the functions run immediately when the program starts, not when you click the buttons. You need to pass the function reference instead, so remove the parentheses: command=calendar_view.
Here's your corrected code
import tkinter as tk from tkinter import ttk from tkcalendar import Calendar, DateEntry def calendar_view(): def print_sel(): print(cal.selection_get()) top = tk.Toplevel(root) cal = Calendar(top, font="Arial 14", selectmode='day', cursor="hand1", year=2018, month=2, day=5) cal.pack(fill="both", expand=True) ttk.Button(top, text="ok", command=print_sel).pack() def dateentry_view(): def print_dateentry_date(): # Get as datetime.date object selected_date = cal.get_date() print("Selected date (datetime object):", selected_date) # Or get as formatted string selected_date_str = cal.get() print("Selected date (string):", selected_date_str) 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) # Add a button to trigger date retrieval ttk.Button(top, text="ok", command=print_dateentry_date).pack(padx=10, pady=5) root = tk.Tk() s = ttk.Style(root) s.theme_use('clam') # Remove parentheses from command arguments ttk.Button(root, text='Calendar', command=calendar_view).pack(padx=10, pady=10) ttk.Button(root, text='DateEntry', command=dateentry_view).pack(padx=10, pady=10) root.mainloop()
What changed:
- Added a
print_dateentry_date()function insidedateentry_view()to handle retrieving and printing the date when the OK button is clicked - Used
get_date()andget()to fetch the selected date in both object and string formats - Fixed the root button commands by removing the parentheses so they only run when clicked
- Removed the incorrect
print(cal.cget(DateEntry))line (that method is for fetching widget configuration options, not user-selected values)
Now when you select a date in the DateEntry widget and click OK, you'll see the selected date printed in your console!
内容的提问来源于stack exchange,提问作者Garima Tiwari

