简体   繁体   English

如何在 tkinter 中仅绑定 ASCII 键?

[英]How to bind ONLY ASCII keys in tkinter?

I want to bind only ASCII keys using tkinter. I know how to bind it selectively (per key) or even by binding it to all keyboard keys (by using <Key> or <KeyPress> ), but problem is, I don't know how to do the same for every ASCII keys.我只想使用 tkinter 绑定 ASCII 键。我知道如何有选择地(按键)绑定它,甚至将它绑定到所有键盘键(通过使用<Key><KeyPress> ),但问题是,我不知道知道如何对每个 ASCII 键执行相同的操作。

Here is what I tried so far:到目前为止,这是我尝试过的:

  1. Using <Key> or <KeyPress> binding for catching all keyboard keys (doesn't support mouse keys):使用<Key><KeyPress>绑定来捕获所有键盘键(不支持鼠标键):
import tkinter as tk

def key_press(event):
    label.config(text = f'char Pressed: {event.char!r}')
    label2.config(text=f'keysym Pressed: {event.keysym!r}')

root = tk.Tk()
label = tk.Label(root, text='Press a key')
label2 = tk.Label(root, text='Press a key')
label.pack()
label2.pack()
root.bind('<Key>', key_press)
root.mainloop()
  1. Using per key binding (need to know the name/keysym first, as seen on the tkinter documentation ):使用每个键绑定(首先需要知道名称/keysym,如tkinter 文档所示):
import tkinter as tk

def key_press(event):
    label.config(text = f'char Pressed: {event.char!r}')
    label2.config(text=f'keysym Pressed: {event.keysym!r}')

root = tk.Tk()
label = tk.Label(root, text='Press a key')
label2 = tk.Label(root, text='Press a key')
label.pack()
label2.pack()
# here we only use the K and BackSpace key as example
root.bind('<BackSpace>', key_press)
root.bind('<K>', key_press)
root.mainloop()

How can I bind a function only to all ascii keys using just tkinter?如何仅使用 tkinter 将 function 绑定到所有 ascii 键? (no third-party module if possible) (如果可能,没有第三方模块)

The easiest and efficient solution to bind only ascii chars in my opinion is to use event.char .在我看来,仅绑定ascii 字符的最简单有效的解决方案是使用event.char event.char corresponds to %A and will be an empty string if no printable character is parsed. event.char对应于%A ,如果没有解析可打印字符,则为空字符串。 To access the individual character you can use repr as it is implemented in tkinter要访问单个字符,您可以使用repr ,因为它在 tkinter 中实现

I did not used event.keycode because for Multi_key they seem unreliable.我没有使用event.keycode因为对于Multi_key它们似乎不可靠。 Also included del -key but found no better way as to use keysym for it, feel free to find a better way还包括del -key 但没有找到更好的方法来使用keysym ,请随意寻找更好的方法

Example code:示例代码:

import tkinter as tk

def isascii(event):
    if event.char != '':
        char = list(event.char)[0] #'^' or '`' is only parsed as doubles in Windows
        if ord(char) <= 127: #avoid non-ascii like '²' or '³' or '´'
            print(
                repr(char), #printable character
                ord(char) #corresponding ascii value
                )
    elif event.keysym == 'Delete':
        print('backspace / delete', 127)

root = tk.Tk()
root.bind('<Key>', isascii)
root.mainloop()

compressed version:压缩版:

import tkinter as tk

def isascii(event):
    if event.char != '' and ord((char := list(event.char)[0])) <= 127:
        print(
            repr(char), #printable character
            ord(char) #corresponding ascii value
            )
    elif event.keysym == 'Delete': print('delete', 127)

root = tk.Tk()
root.bind('<Key>', isascii)
root.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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