主题tkinter中的Numberpad绑定

2024-10-05 14:30:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直试图使用themed tkinter库在我的python程序中bindnumberpad键,但没有成功。我在effbot上查找了文档,但是没有提到绑定num pad键。所提到的事件绑定仅包括字母数字键(字母键上方)、鼠标按钮

以下代码绑定位于alohabet键上方的数字键:

>>> Frame.bind("1", callback) # binds Key 1, not num-pad Key 1

如何绑定数字键盘键


Tags: key文档程序tkinter字母事件鼠标按钮
2条回答

在本报告中{a1}提到:

For example, the digit 2 on the numeric keypad (key symbol KP_2) and the down arrow on the numeric keypad (key symbol KP_Down) have the same key code (88), but different .keysym_num values (65433 and 65458, respectively).

因此,您可以尝试使用:

Frame.bind("<KP_1>", callback)

但是,这在我的电脑上无法工作。另一种方法是绑定所有键,并检查keycode以了解是否按了num pad键1:

def callback(e):
    if e.keycode == 97:
        print("you pressed num-pad 1")

Frame.bind("<Key>", callback)

正如@jizhhoasama已经提到的:

def callback(e):
    if e.keycode == 97:
        print("you pressed num-pad 1"))

并非所有系统都能保证相同的键码值(Windows中“1”的键码为“97”,Linux中为“87”)

因此,可以使用char代替keycode

def callback(e):
    if int(e.char) in range(10):
        print(f'you pressed {e.char}')

注意:在按下数字键时也会调用回调(因为它们具有相同的字符值)。在某些情况下,这可能是可取的

相关问题 更多 >