python - 如何使两台印刷机在 tkinter 中产生相同的输出
问题描述
我想制作一个将两个输入绑定到一个输出的 Tkinter 程序我试过这个:
def hh(event):
print('hello')
root.bind(<Returna>, hh)
和
def hh(event):
print('hello')
root.bind('<Return, KeyPress-a>')
但它没有按预期工作。有人可以告诉我如何制作<Shift>
并<KeyPress-a>
一起触发hh()
吗?谢谢!
第一个错误
Traceback (most recent call last):
File "tktest.py", line 19, in <module>
root.bind('<Returna>', hh)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/`__init__.`py", line 1251, in bind
return self._bind(('bind', self._w), sequence, func, add)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/`__init__`.py", line 1206, in _bind
self.tk.call(what + (sequence, cmd))
_tkinter.TclError: bad event type or keysym "Returna"
第二个错误
Traceback (most recent call last):
File "tktest.py", line 19, in <module>
root.bind('<Return, KeyPress-a>', hh)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 1251, in bind
return self._bind(('bind', self._w), sequence, func, add)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 1206, in _bind
self.tk.call(what + (sequence, cmd))
_tkinter.TclError: bad event type or keysym "Return,"
解决方案
您必须使用字符串'<Return>a'
并添加函数名称
root.bind('<Return>a', hh)
但是当您直接hh()
按下后它会运行a
Enter
import tkinter as tk
def hh(event):
print('hello')
root = tk.Tk()
root.bind('<Return>a', hh)
root.mainloop()
如果您想hh()
在按Enter
OR时运行,a
那么您需要两个绑定
root.bind('<Return>', hh)
root.bind('a', hh)
import tkinter as tk
def hh(event):
print('hello')
root = tk.Tk()
root.bind('<Return>', hh)
root.bind('a', hh)
root.mainloop()
编辑:
在已编辑的问题中,我看到<Shift>
了,所以也许必须如此Shift + a
?它需要A
root.bind('A', hh) # Shift + a
在我看到的第二个错误中,Return,
但也许它必须是Shift + ,
具有特殊名称的<less>
root.bind('<less>', hh) # Shift + ,
在 asnwer 到.bind() 不适用于 shift 键绑定?我展示了显示keysym
按键的代码以及您可以在其中使用的代码bind()
import tkinter as tk
def test(event):
print('keysym:', event.keysym)
root = tk.Tk()
root.bind('<Key>', test)
root.mainloop()
您可以在文档Tcl/Tk -keysym中找到一些名称
但是or并没有什么特别之处keysym
,因为它是不寻常的组合,可能需要按住/再按or 。或按下、松开,然后按下或Return + a
Return + ,
Return
Enter
a
,
Return
Return
a
,
推荐阅读
- python - 启用在 python 中导入烧瓶
- css - WordPress / Gutenberg - 一个文件中的前端和编辑器样式?
- mysql - 需要mysql查询来根据匹配的查询找到总和
- python - 使用 python 时,输出窗口在 Visual Studio 代码中不起作用
- go - GO 用 int 连接字符串
- django - Django模型中的多个自引用ForeignKey关系字段
- javascript - 试图确定对象的“内部”属性是否存在
- javascript - insertAdjacentHTML is not a function in todolist
- regex - Google Sheet:过滤单个单元格中的多个值
- mysql - 选择查询 Yii2 中的左连接 - 'on 子句'中的未知列'single'