Closed sim2511 closed 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.
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')
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()
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()
Hello,
my interface.py
my main.py
I would like to launch open_w2 function when i press enter key button, this code not works, any idea why? thanks