繁体   English   中英

将多个键绑定到 Tkinter Python 中的函数

[英]Binding multiple keys to a function in Tkinter Python

假设我想将我的空格键和键“w”绑定到我的代码中的任何随机函数,我该怎么做? 我应该使用 if "w" 和 if "" then perform 还是你可以将多个键绑定到一个功能?

#if statement way. idk how to do this tho
if "w" == Pressed:
   if "<space>" == Pressed:
      #perform function

#or 
self._master.bind("<space>", "w", lambda e: function)

tkinter您可以将字符串与所有键"<space>w"放在一起,您可以执行以下操作:按空格,(释放空格或不释放空格),按w它将运行功能。

import tkinter as tk

def test(event):
    print('test')

root = tk.Tk()

root.bind('<space>w', test)

root.mainloop()

通过添加和,我能够将此文本输入框绑定到选项卡和输入键。 我想如果你想让每个键运行不同的功能,你可以。

Text.bind("<Tab>", AddText) and Text.bind("<Return>", AddText)

暂无
暂无

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

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