【问题标题】:Binding multiple keys to a function in Tkinter Python将多个键绑定到 Tkinter Python 中的函数
【发布时间】:2019-05-19 02:44:17
【问题描述】:

假设我希望将空格键和键“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)

【问题讨论】:

  • 你用什么? tkinterPyQtPySidewxPython,其他?
  • 我目前在python中使用tkinter来执行这个功能
  • 然后提出问题,以便其他人可以看到。
  • 道歉。我是使用堆栈交换的新手。

标签: python if-statement tkinter bind


【解决方案1】:

tkinter你可以把字符串和所有的键"&lt;space&gt;w"一起放,你可以做:按空格,(是否释放空格),按w,它会运行函数。

import tkinter as tk

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

root = tk.Tk()

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

root.mainloop()

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 这不能回答问题,你不应该这样使用和喜欢
    猜你喜欢
    • 1970-01-01
    • 2013-03-06
    • 2013-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-23
    • 2012-10-30
    相关资源
    最近更新 更多