简体   繁体   中英

Tkinter <KP_Enter> binding unresponsive

I have read a fair few other threads that seemed to be dealing with this problem, but none of them gave me a fix. The code for the button in question is:

#equals button
cmd = lambda x='=': self.Click(x)
self.equals_button = Button(self.MasterFrame, text='=', width=12,
                            height=2, command=cmd)
self.equals_button.grid(row=4, column=3, columnspan=3, sticky=W+E+N+S,
                        ipadx=2, padx=2, pady=2)
self.equals_button.bind("<KP_Enter>", cmd)

I tried calling .focus_set() on the button at various stages in the code but to no avail(it didn't work). This code appears in the __init__ method. Earlier in the method, I call .focus_set() on another widget instance which is necessary. I tried calling this bind on the Master frame also, along with focus_set both on the Master frame and the button itself. None of this has helped. I am not getting any error messages. The button does work when I mouse-click on it. It simply doesn't respond to pressing the KP_Enter. I would like it to do both.

With the help of Kevin I resolved this issue. Binding to <KP_Enter> wasn't working, but binding instead to <Return> was responding, although, still not working correctly. So, Instead of binding the self.equals_button , I bound the Entry widget itself to <Return> , such that it only responds to the key-press when it is focused upon with .focus_set() . The data in the Entry widget was used as a param for the function in the .bind() params. So now I have the same function bound, first to the Entry widget when I press Enter(this includes keypad enter on my system, which is what I wanted), and again in the widget instantiation statement( button=Button(etc.etc.command=myfunc) ), allowing me to have the same functionality by clicking with the mouse and by pressing the keypad enter key. Thanks again Kevin.

You probably code under windows. KP_Enter is only valid on Unix (linux, bsd...). Under windows, Return and KP_Enter are the same key: Return

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM