在Tkinter Python中将多个键绑定到一个函数 [英] Binding multiple keys to a function in Tkinter Python

查看:25
本文介绍了在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()

这篇关于在Tkinter Python中将多个键绑定到一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆