LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Python : How to bind tkinter widget to numeric enter key (https://www.linuxquestions.org/questions/programming-9/python-how-to-bind-tkinter-widget-to-numeric-enter-key-4175640691/)

Roihan 10-18-2018 11:43 PM

Python : How to bind tkinter widget to numeric enter key
 
I'm creating a simple python program and I need to bind my tkinter widget to the right enter key (the one which is located in the keyboard numeric pad)

I've browsed google and found the <Return> key binding but it works only for the main enter key.

I really need it because my users will use the numeric keypad for the main operation.

Thanks for any help

RockDoctor 10-19-2018 11:43 AM

Have not tried using it in Python, but xev shows keysym 0xff0d for the return key by the alpha keys, and 0xff8d for the keypad return key

Beryllos 10-21-2018 09:49 AM

The key is called KP_Enter. See this reference:

http://infohost.nmt.edu/tcc/help/pub...key-names.html

(found by googling "python tkinter bind numeric return"
The table contains much more that may interest you.)

average_user 10-21-2018 10:25 AM

It's also KP_Enter according to xev:
Code:

KeyRelease event, serial 36, synthetic NO, window 0x1e00001,
    root 0xd6, subw 0x0, time 1807948071, (1003,272), root:(2923,272),
    state 0x0, keycode 104 (keysym 0xff8d, KP_Enter), same_screen YES,
"  XLookupString gives 1 bytes: (0d) "
    XFilterEvent returns: False

It probably stands for Keypad Enter.

Beryllos 10-21-2018 02:15 PM

Besides looking it up online, another way to discover the name of the key is to inspect the event.keysym attribute in the event handler. Here is a Python 2 program which works for almost any key pressed:

Code:

from Tkinter import *

root = Tk()

def key(event):
    print "pressed", event.keysym

def callback(event):
    frame.focus_set()
    print "clicked at", event.x, event.y

frame = Frame(root, width=100, height=100)
frame.bind("<Key>", key)
frame.bind("<Button-1>", callback)
frame.pack()

root.mainloop()

Typical output:
Code:

$ python2 keypress.py
clicked at 71 58
pressed 'a'
pressed 'semicolon'
pressed 'Return'
pressed 'KP_Enter'
pressed 'KP_Down'
pressed 'KP_Next'
pressed 'Shift_L'
pressed 'Shift_R'
pressed 'Caps_Lock'
pressed 'Control_L'
pressed 'Alt_R'
pressed 'Super_R'

With my particular desktop (gnome 3), this program does not report the PrintScreen button, and only reports the left "Windows Logo" key (keysym Super_L) when Shift, Alt, or Control is pressed at the same time.

Roihan 10-24-2018 06:48 AM

It works well with KP_Enter. I'll learn about xev soon.
Thanks for everybody.


All times are GMT -5. The time now is 04:43 PM.