4

我在 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)
4

2 回答 2

9

不幸的是,您在某种程度上受制于系统的底层自动重复机制。例如,在我目前使用的 mac 上,如果我按住“w”,我将获得新闻和发布事件流。按下时,如果我按下“o”,我会得到一连串针对“o”的按下和释放,但没有更多针对“w”的事件。

您将需要设置一个迷你状态机,并绑定到按键和按键释放事件。这将让您跟踪哪些键被按下,哪些没有被按下。然后,每次绘制框架时,您都可以查询机器以查看按下了哪些键并采取相应措施。

这是我拼凑的一个快速技巧。我只在我的 Mac 上测试过它,而且只在 python 2.5 上测试过。我没有真正尝试过“pythonic”或高效。该代码仅用于说明该技术。使用此代码,您可以同时按“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()
于 2010-01-26T17:03:27.437 回答
-1

您可以绑定到“<Key>”,然后检查 event.char 并根据它的值执行您想要的操作?当然,我不知道当同时按下多个键时这是否有效,它可能仍然会遇到完全相同的问题。我好久没用过Tk了。

“<Key> 用户按下了任意键。键在传递给回调的事件对象的 char 成员中提供(对于特殊键,这是一个空字符串)。”

于 2010-01-26T15:23:19.723 回答