CustomTkinter
CustomTkinter copied to clipboard
Binding keys
Good Day, Firstly I'll like to say well done Mr. Schimansky and thank you very much for creating such a wonderful framework. I enjoy using the widgets you've designed. It's very impressive work. Currently right now I'm working on a project whereby users must have short-cut keys on the desktop to carry out specific tasks. I'm having significant problem binding keys using your framework. Do you know of any workaround for this? Your assistance will be much appreciated.
Regards, Kevin
Can you send any code examples, regarding what you're having problems with?
Here is a snippet of code. As an example let's say I want to bind the enter key to a login button
# login button
send_icon = icon("paper-plane", fill="white", scale_to_width=10)
login_btn = ct.CTkButton(self.login_form_frame, text="Login", width=315, text_color="white", image=send_icon, command=lambda: self.login_user())
login_btn.bind('<Return>', lambda event: self.login_user())
login_btn.pack(pady=(10, 100), ipady=5)
And below is the function login_user that is bind to the login btn
def login_user(self, event=None):
email = self.email_value.get()
password = self.password_value.get()
.....................
The above code should work in regular tkinter but it doesn't appear to work with CustomTkinter. Am I doing something wrong or is there an alternative solution to this problem?
Regards, Kevin
You can just replace the line login_btn.bind('<Return>', lambda event: self.login_user())
to login_btn.bind('<Return>', self.login_user)
there is no need to use the lambda function unless you are taking any arguments(which you're not, in this case)
That's what I had in the beginning. That doesn't work.
I'm having the same problem, the bind method doesn't seem to work, no error appears in the terminal, it just doesn't execute the function when the command is sent.
Hello, not entirely sure but when I tested with normal tkinter just pressing return did not work, I had to keep clicking tab till the button was highlighted or focused then it worked fine. I had to bind the parent window which worked for both tkinter and customtkinter, might not be a perfect solution but it works for now. It works with top level and the root tkinter/customtkinter window.
send_icon = icon("paper-plane", fill="white", scale_to_width=10)
login_btn = ct.CTkButton(self.login_form_frame, text="Login", width=315, text_color="white", image=send_icon,
command=lambda: self.login_user())
'Parent window of self.login_form_frame here'.bind('<Return>', self.login_user)
login_btn.pack(pady=(10, 100), ipady=5)
You could also set the takefocus=False for the widgets that you obtain the email and password when they are declared, return would work but only when you press tab, this method is annoying if you have multiple widgets in your frame. Assuming they are entry widgets it would be something like this:
password = ct.CTkEntry(....., takefocus=False)
email= ct.CTkEntry(....., takefocus=False)
And your code would stay the same
# login button
send_icon = icon("paper-plane", fill="white", scale_to_width=10)
login_btn = ct.CTkButton(self.login_form_frame, text="Login", width=315, text_color="white", image=send_icon, command=lambda: self.login_user())
login_btn.bind('<Return>', self.login_user)
login_btn.pack(pady=(10, 100), ipady=5)
The bind method will be implemented for all CTk widgets in the next major version 5.0.0, which will be released soon.
Looking forward to it. Thank you very much for sharing your work.
Binding is now implemented with version 5.0.2.