Python 绑定 - 允许同时按下多个键

4 投票
2 回答
9732 浏览
提问于 2025-04-15 18:28

我在用Python的时候遇到了一个问题。

我正在使用Tkinter这个库,并且设置了四个事件来监听我表单上的按键。

我的问题是,这些事件不能同时运行。举个例子,我可以按下一个按键,事件会被识别。但是当我同时按住两个按键时,只有一个事件会被触发。

有没有其他的方法可以解决这个问题呢?

    self.f.bind("w", self.player1Up)
    self.f.bind("s", self.player1Down)
    self.f.bind("o", self.player2Up)
    self.f.bind("l", self.player2Down)

2 个回答

-1

你可以绑定到 "<Key>",然后检查事件中的 event.char,根据这个值来执行你想要的操作?不过,我不太确定当同时按下多个键时,这样是否有效,可能还是会遇到同样的问题。我已经很久没用 Tk 了。

"<Key> 表示用户按下了任意一个键。这个键的信息会在传递给回调函数的事件对象的 char 成员中提供(对于特殊键,这里是一个空字符串)。"

9

很遗憾,你的系统在自动重复按键的机制上有点受制于人。比如说,我现在用的这台Mac,如果我按住“w”键,就会不断地出现按下和松开的事件。在我按住“w”的同时,如果我按“o”键,就会出现“o”的按下和松开的事件,但“w”就不会再有新的事件了。

你需要建立一个简单的状态机,并绑定按键按下和松开的事件。这样你就可以跟踪哪些键被按下,哪些没有。然后,每次绘制画面时,你可以查询这个状态机,看看哪些键被按下,并相应地进行处理。

这里有一个我快速写的小程序。我只在我的Mac上测试过,而且只用的是Python 2.5。我并没有特别追求“Python风格”或效率。这段代码只是用来展示这个技术。通过这段代码,你可以同时按下“w”或“s”以及“o”或“l”来上下移动两个挡板。

'''Example that demonstrates keeping track of multiple key events'''
from Tkinter import *

class Playfield:
    def __init__(self):
        # this dict keeps track of keys that have been pressed but not
        # released
        self.pressed = {}

        self._create_ui()

    def start(self):
        self._animate()
        self.root.mainloop()

    def _create_ui(self):
        self.root = Tk()
        self.p1label = Label(text="press w, s to move player 1 up, down", 
                             anchor="w")
        self.p2label = Label(text="press o, l to move player 2 up, down", 
                             anchor="w")
        self.canvas = Canvas(width=440, height=440)
        self.canvas.config(scrollregion=(-20, -20, 420, 420))

        self.p1label.pack(side="top", fill="x")
        self.p2label.pack(side="top", fill="x")
        self.canvas.pack(side="top", fill="both", expand="true")

        self.p1 = Paddle(self.canvas, tag="p1", color="red", x=0, y=0)
        self.p2 = Paddle(self.canvas, tag="p2", color="blue", x=400, y=0)

        self._set_bindings()

    def _animate(self):
        if self.pressed["w"]: self.p1.move_up()
        if self.pressed["s"]: self.p1.move_down()
        if self.pressed["o"]: self.p2.move_up()
        if self.pressed["l"]: self.p2.move_down()
        self.p1.redraw()
        self.p2.redraw()
        self.root.after(10, self._animate)

    def _set_bindings(self):
        for char in ["w","s","o", "l"]:
            self.root.bind("<KeyPress-%s>" % char, self._pressed)
            self.root.bind("<KeyRelease-%s>" % char, self._released)
            self.pressed[char] = False

    def _pressed(self, event):
        self.pressed[event.char] = True

    def _released(self, event):
        self.pressed[event.char] = False

class Paddle():
    def __init__(self, canvas, tag, color="red", x=0, y=0):
        self.canvas = canvas
        self.tag = tag
        self.x = x
        self.y = y
        self.color = color
        self.redraw()

    def move_up(self):
        self.y = max(self.y -2, 0)

    def move_down(self):
        self.y = min(self.y + 2, 400)

    def redraw(self):
        x0 = self.x - 10
        x1 = self.x + 10
        y0 = self.y - 20
        y1 = self.y + 20
        self.canvas.delete(self.tag)
        self.canvas.create_rectangle(x0,y0,x1,y1,tags=self.tag, fill=self.color)

if __name__ == "__main__":
    p = Playfield()
    p.start()

撰写回答