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

How to create a clicked function in tkinter calendar?

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

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")  

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

Calendar 小部件是标签的框架 filled/covered,这就是绑定不起作用的原因,因为双击事件由这些标签而不是日历小部件消耗。

查看 tkcaender.Calendar 的代码,实例变量 _calendar(二维列表)用于存储这些标签。所以你可以在这些标签上绑定 <Double-1>

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