CustomTkinter icon indicating copy to clipboard operation
CustomTkinter copied to clipboard

Binding keys

Open kdc1919 opened this issue 2 years ago • 8 comments

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

kdc1919 avatar Aug 10 '22 14:08 kdc1919

Can you send any code examples, regarding what you're having problems with?

TheFallen-Cat avatar Aug 10 '22 16:08 TheFallen-Cat

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

kdc1919 avatar Aug 10 '22 16:08 kdc1919

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)

TheFallen-Cat avatar Aug 11 '22 06:08 TheFallen-Cat

That's what I had in the beginning. That doesn't work.

kdc1919 avatar Aug 11 '22 14:08 kdc1919

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.

CarlosHGL avatar Aug 14 '22 18:08 CarlosHGL

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)

Mohammed-Ibrahim99 avatar Aug 15 '22 14:08 Mohammed-Ibrahim99

The bind method will be implemented for all CTk widgets in the next major version 5.0.0, which will be released soon.

TomSchimansky avatar Oct 07 '22 14:10 TomSchimansky

Looking forward to it. Thank you very much for sharing your work.

kdc1919 avatar Oct 12 '22 15:10 kdc1919

Binding is now implemented with version 5.0.2.

TomSchimansky avatar Dec 06 '22 22:12 TomSchimansky