python - 如何在 tkinter 日历中创建点击功能?

标签 python user-interface tkinter tkcalendar

我正在尝试为我的日历创建一个点击功能。

class MainWindow():
    
    def __init__(self, app) :
        
        #---------------------------------Initialisation de la page principale------------------------------------
        self.app = app
        self.app.title("Page Principale")

        # Center main window
       #----------------------------------------------------------------
        app_width = 800
        app_height = 600

        screnn_width = app.winfo_screenwidth()
        screnn_heigth = app.winfo_screenheight()

        x = (screnn_width / 2) - (app_width / 2)
        y = (screnn_heigth / 2) - (app_height / 2)

        self.app.geometry(f'{app_width}x{app_height}+{int(x)}+{int(y)}')
       #----------------------------------------------------------------
        self.app.config(background="azure")
        self.app.resizable(0,0)
        self.app.focus_force()
        
        today = datetime.date.today()
        self.cal = Calendar(app, selectmode="day", year=today.year, month=today.month, day=today.day, date_pattern="mm/dd/yyyy")
        self.cal.pack(ipady=20, fill="both", expand=True)        
       
        self.cal.bind('<Double-1>', self.double_click)

这是我的 double_click 函数:

def double_click(self, event):
        print("event double click effectuer")  

问题是我的函数没有执行 我希望当我在日历中时,当我双击时,我会显示消息。 但仅限于日历,而不是应用程序的其余部分。 目标是针对特定的一天,当用户双击时,将打开一个模式窗口,其中包含他单击当天的许多信息 感谢您的帮助!

最佳答案

Calendar小部件是一个被标签填充/覆盖的框架,这就是绑定(bind)不起作用的原因,因为双击事件是由这些标签而不是日历小部件消耗的。

查看tkcaender.Calendar的代码,实例变量_calendar (2D列表)用于存储这些标签。这样就可以绑定(bind) <Double-1>在这些标签上:

for row in self.cal._calendar:
    for lbl in row:
        lbl.bind('<Double-1>', self.double_click)

关于python - 如何在 tkinter 日历中创建点击功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71307397/

相关文章:

python - 使用 Python 请求响应 405

python - 如何在特定轴上连接张量列表?

python - scipy BSpline 在 python 中的拟合

java - 基本的 Java GUI 设计

安卓。如何从我的应用程序中读取 Aztec 代码?

python - 在 Tkinter 中交换

python - 如何使用 tkinter 在图像顶部绘制坐标系?

python - tkinter按钮限制命令不重复

python - PyFPDF 在编码为字节字符串后返回空白 pdf

用于 Mac OS X 的 Python GUI 框架