Gar*_*ari 4 python tkinter python-3.x tkcalendar
我有一个 tkcalendar,它是日历、DateEntry 的预定义小部件,我正在尝试获取用户为 DateEntry 选择的日期。 虽然可以使用“selection_get()”提取日历小部件的选定日期,但我找不到 DateEntry 的任何内容。
我试过 get_date(),get(),_date(), cget(), ._selection() 等等,但它们似乎没有返回/打印用户选择的日期。请帮忙,如果需要任何补充信息,请告诉我
代码[选自一个简单的 tkcalendar 教程]:
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():
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)
print(cal.cget(DateEntry))
root = tk.Tk()
s = ttk.Style(root)
s.theme_use('clam')
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()
Run Code Online (Sandbox Code Playgroud)
您提到您已经尝试过get_date()
但没有奏效,但这实际上是正确的功能。
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_sel():
print(cal.get_date())
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)
ttk.Button(top, text="ok", command=print_sel).pack()
root = tk.Tk()
s = ttk.Style(root)
s.theme_use('clam')
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()
Run Code Online (Sandbox Code Playgroud)
如果您想在每次更改时获取日期,您可以使用事件绑定。从文档:
- 虚拟活动
<<CalendarSelected>>
每次用户用鼠标选择一天时都会生成一个事件。
因此,您可以将获取日期的函数绑定到<<CalendarSelected>>
事件:
def dateentry_view():
def print_sel(e):
print(cal.get_date())
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)
cal.bind("<<DateEntrySelected>>", print_sel)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
13846 次 |
最近记录: |