如何将空格键绑定到 tkinter (python) 中的某个方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15753793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:54:56  来源:igfitidea点击:

How to bind spacebar key to a certain method in tkinter (python)

pythonmethodspython-2.7tkinterpython-imaging-library

提问by

I am working on a project in python, and I made a method to draw a specific thing in tkinter. I want it so that whenever I press the spacebar, the image will redraw itself (run the method again because I coded the method so that it could redraw over itself). How exactly would I bind the spacebar to the method so that the program would run, draw, and re-draw if I pressed the spacebar?

我正在用 python 做一个项目,我做了一个方法来在 tkinter 中绘制一个特定的东西。我想要它,这样每当我按下空格键时,图像就会重绘自己(再次运行该方法,因为我对方法进行了编码,以便它可以重绘自己)。如果我按下空格键,我将如何将空格键绑定到该方法,以便程序运行、绘制和重新绘制?

for example, i want it so that whenever I press space, the program draws in a random location on the canvas:

例如,我想要这样,每当我按下空格时,程序都会在画布上的随机位置进行绘制:

from Tkinter import *
from random import *

root=Tk()
canvas=Canvas(root,width=400,height=300,bg='white')
def draw():
    canvas.delete(ALL)# clear canvas first
    canvas.create_oval(randint(0,399),randint(0,299),15,15,fill='red')
draw()
canvas.pack()
root.mainloop()

how would i bind the spacebar to the method?

我将如何将空格键绑定到该方法?

采纳答案by twasbrillig

from Tkinter import *
from random import *

root=Tk()
canvas=Canvas(root,width=400,height=300,bg='white')
def draw(event=None):
    canvas.delete(ALL)# clear canvas first
    canvas.create_oval(randint(0,399),randint(0,299),15,15,fill='red')
draw()
canvas.pack()

root.bind("<space>", draw)
root.mainloop()

回答by Michael0x2a

You could do something like this:

你可以这样做:

from Tkinter import *
from random import *

root=Tk()
canvas=Canvas(root,width=400,height=300,bg='white')

def draw(event):
    if event.char == ' ':
        canvas.delete(ALL)# clear canvas first
        canvas.create_oval(randint(0,399),randint(0,299),15,15,fill='red')

root.bind('<Key>', draw)

canvas.pack()
root.mainloop()

Basically, you bind your drawing function to some top-level element to the <Key>binding which is triggered whenever a key on the keyboard is pressed. Then, the event object that's passed in has a charmember which contains a string representing the key that was pressed on the keyboard.

基本上,您将绘图函数绑定到某个顶级元素,该<Key>绑定会在按下键盘上的键时触发。然后,传入的事件对象有一个char成员,该成员包含一个字符串,表示在键盘上按下的键。

The event will only be triggered when the object it's bound to has focus, which is why I'm binding the drawmethod to the rootobject, since that'll always be in focus.

只有当它绑定到的对象具有焦点时才会触发该事件,这就是我将draw方法绑定到root对象的原因,因为它始终处于焦点。

回答by Alpacah

You can aslo use canvas.bind_all("<space>", yourFunction)That will listen for events in the whole application and not only on widget.

您也可以使用canvas.bind_all("<space>", yourFunction)That 将侦听整个应用程序中的事件,而不仅仅是在小部件上。