Tkcalendar是一个Python的日期选择器库,它基于Tkinter库,用于创建日期选择器的GUI界面。在运行代码后保持打开Tkcalendar,可以通过以下步骤实现:
import tkinter as tk
from tkcalendar import Calendar
window = tk.Tk()
window.title("Tkcalendar Example")
def open_calendar():
def get_selected_date():
selected_date = cal.get_date()
print("Selected Date:", selected_date)
top.destroy()
top = tk.Toplevel(window)
cal = Calendar(top, selectmode="day")
cal.pack()
select_button = tk.Button(top, text="Select", command=get_selected_date)
select_button.pack()
open_button = tk.Button(window, text="Open Calendar", command=open_calendar)
open_button.pack()
window.mainloop()
完整的代码示例:
import tkinter as tk
from tkcalendar import Calendar
window = tk.Tk()
window.title("Tkcalendar Example")
def open_calendar():
def get_selected_date():
selected_date = cal.get_date()
print("Selected Date:", selected_date)
top.destroy()
top = tk.Toplevel(window)
cal = Calendar(top, selectmode="day")
cal.pack()
select_button = tk.Button(top, text="Select", command=get_selected_date)
select_button.pack()
open_button = tk.Button(window, text="Open Calendar", command=open_calendar)
open_button.pack()
window.mainloop()
运行以上代码后,点击"Open Calendar"按钮,将会弹出一个新窗口,其中包含一个可选择日期的Tkcalendar。选择日期后,点击"Select"按钮,将会打印所选日期,并关闭Tkcalendar窗口。
腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云