9

I am developing a program to control a machine that will have only a keypad connected. I am using Python 2.7 and Tkinter 8.5. I am using OptionMenus to allow user to do setup on machine.

When I run under Windows I am able to use arrow keys on keypad to traverse thru drop down list, then use key pad enter to pick option. This is not working on Linux (Debian Wheezy).

How do I bind KP_Enter to behave as return key?

import Tkinter

def c(self, event):
   event.b[".keysym"] = "<<space>>"
   print "button invoked"

t = Tkinter.Tk()

b = Tkinter.OptionMenu(t, ".500", ".510", ".520", 
                       ".550", ".560", ".570", ".580", command=c)
t.bind("<KP_Enter>", c)
e = Tkinter.Entry()
e.pack()
b.pack(anchor=Tkinter.E)

t.mainloop()
ncica
  • 7,015
  • 1
  • 15
  • 37
pglitt
  • 91
  • 6
  • have you tried creating a binding to ``? Your code only shows a binding on ``. – Bryan Oakley Mar 31 '14 at 10:58
  • I posted the wrong bit of code, but it dose not work with KP_Enter. I have been studying and am using the following to bind KP_up and KP_down arrows def focus_next_button(event): event.widget.tk_focusNext().focus() return("break") master.bind("", focus_next_button) def focus_prev_button(event): event.widget.tk_focusPrev().focus() return("break") master.bind("", focus_prev_button) – pglitt Apr 02 '14 at 01:03
  • Bryan I think I could work if I knew a way to make tk.focus_next work in drop down list. I am also considering changing the binding in tkinter module , but I am not sure if that is a good idea. – pglitt Apr 02 '14 at 01:16

1 Answers1

1

With this script (from here), it should be easy to identify the key event triggered by Tkinter when you press any key, whether that is <Return>, <KP_Enter>, or (somehow, maybe your keypad has a funny mapping) something else.

Just look at the console output when you press the desired button, and use that key event name in your actual code.

import Tkinter

def callback(e):
    print e.keysym

w = Tkinter.Frame(width=512, height=512)
w.bind("<KeyPress>", callback)
w.focus_set()
w.pack()
w.mainloop()
Junuxx
  • 14,011
  • 5
  • 41
  • 71