Home > database >  How to create a clicked function in tkinter calendar?
How to create a clicked function in tkinter calendar?

Time:03-03

I am trying to create a clicked function to my 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)

Here is my double_click function :

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

The problem is that my function is not executed I want that when i am in the calendar, when i double click, i have the message that appear. But only in the calendar, not in the rest of the app. The goal is for a specific day, when the user double click, that will open a modal window with many infos of the day that he clicked Thx for your help!

CodePudding user response:

The Calendar widget is a frame filled/covered by labels, that's why the binding does not work because the double click event is consumed by those labels, not the calendar widget.

Looking into the code of tkcaender.Calendar, instance variable _calendar (2D list) is used to store those labels. So you can bind <Double-1> on those labels:

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