TomSchimansky / CustomTkinter

A modern and customizable python UI-library based on Tkinter
MIT License
10.8k stars 1.03k forks source link

Bind "Enter Key" to button does not work #2369

Closed sim2511 closed 2 months ago

sim2511 commented 2 months ago

Hello,

my interface.py

class W1(customtkinter.CTk):
    def __init__(self):
        super().__init__()
        customtkinter.set_appearance_mode("Light")
        self.geometry("550x150")
        self.title("W1")
        self.button = customtkinter.CTkButton(self, text="W1", command=lambda:self.open_w2())
        self.button.grid(row=0,column=0)
        self.button.bind('<Return>', self.open_w2)

    def open_w2(self):
        print('hello')

my main.py

if __name__ == "__main__":
    root_1 = W1()
    root_1.mainloop() 

I would like to launch open_w2 function when i press enter key button, this code not works, any idea why? thanks

dipeshSam commented 2 months ago

@sim2511 Button should be focused first. Use .focus() before detecting the pressing action.


...
self.button.focus()

Note that every time you need to focus the button whenever you want to listen key events from this button.

sim2511 commented 2 months ago

Hello @dipeshSam thanks for your reply

i tried but still not works, i'm on 5.2.0 customtkinter version, the focus method just return "none"

class W1(customtkinter.CTk):
    def __init__(self):
        super().__init__()
        customtkinter.set_appearance_mode("Light")
        self.geometry("550x150")
        self.title("W1")

        self.button = customtkinter.CTkButton(self, text="W1", command=self.open_w2)
        self.button.grid(row=0, column=0)
        self.button.focus()
        self.button.bind('<Return>', self.open_w2)

    def open_w2(self):
        print('hello')
sim2511 commented 2 months ago

I found a solution, hope it can help someone else ! (add event=None in your function parameter) and try with bind_all method instead of bind, it worked for me:

class W1(customtkinter.CTk):
    def __init__(self):
        super().__init__()
        customtkinter.set_appearance_mode("Light")
        self.geometry("550x150")
        self.title("W1")

        self.button = customtkinter.CTkButton(self, text="W1", command=self.open_w2)
        self.button.grid(row=0, column=0)
        self.bind_all('<Return>', self.open_w2)

    def open_w2(self, event=None):
        print('hello')

if __name__ == "__main__":
    root_1 = W1()
    root_1.mainloop() 
sim2511 commented 2 months ago

also work with bind, the event parameters is the only thing you need to do

class W1(customtkinter.CTk):
    def __init__(self):
        super().__init__()
        customtkinter.set_appearance_mode("Light")
        self.geometry("550x150")
        self.title("W1")

        self.button = customtkinter.CTkButton(self, text="W1", command=self.open_w2)
        self.button.grid(row=0, column=0)
        self.bind('<Return>', self.open_w2)

    def open_w2(self, event=None):
        print('hello')

if __name__ == "__main__":
    root_1 = W1()
    root_1.mainloop()