【发布时间】:2021-03-27 19:33:19
【问题描述】:
疑问如下,我想使用tkcalendar库选择一个日期,它选择正确,但我不能在函数外使用变量。
def dateentry_view():
def print_sel():
date = cal.get_date()
print(date)
top = tk.Toplevel(root)
ttk.Label(top, text='Elige el día').pack()
cal = DateEntry(top)
cal.pack(padx=10, pady=10)
ttk.Button(top, text="Aceptar", command=print_sel).pack()
如何传递date 变量以将其显示在Label 中,如下所示:
labelDate = Label(root,textvariable=date)
我尝试将Label 放入函数中,但它仍然没有显示date 变量。
def dateentry_view():
top = tk.Toplevel(root)
ttk.Label(top, text='Elige el día').pack()
cal = DateEntry(top)
cal.pack(padx=10, pady=10)
ttk.Button(top, text="Aceptar", command=print_sel).pack()
def print_sel():
date = cal.get_date()
print(date)
labelFecha = Label(root,textvariable=date)
当我打印 date 时,它会显示我正确选择的日期。
【问题讨论】:
-
了解 Python 关键字
global和nonlocal。 -
global/nonlocal这里不需要!每当您使用这些关键字时,您可能让自己承受痛苦。 -
@MichaelButscher 从来不知道有
nonlocal关键字。介意解释一下吗? -
@TheLizzard:您可以在documentation 中了解它。
标签: python date datetime tkinter tkcalendar