如何修复 tkinter 中的键绑定

How to fix key bindings in tkinter

我想让精灵在 canvas 上四处移动,并尝试使用键绑定作为控件。当我 运行 程序在我尝试正确的键之前它不会移动。我已经用鼠标按钮进行了测试,效果很好。

添加代码:

from tkinter import *

class MainGame:
    def __init__(self):
        self.grid = [[""]*15 for n in range(15)]
        self.grid[14][3] = 1
        print(self.grid)
        self.canvas = Canvas(root, width = 900, height = 900)
        self.canvas.pack()

        self.a, self.b = 45, 175

    def noreaction(self, event):
        print("It clicked")
        print(self.a)
        self.a += 50
        self.b += 50
        self.canvas.create_image(self.a, self.b, image = self.pl, tags = "p2Tag")
        self.canvas.delete("p1Tag")
        self.canvas.tag_bind("p2Tag", "<Key-q>", self.noreaction)

    def run(self):
        self.pl = PhotoImage(file = "player.png")
        self.canvas.create_image(self.a, self.b, image = self.pl, tags = "p1Tag")
        self.canvas.tag_bind("p1Tag", "<Key>", self.noreaction)

        self.x0, self.y0, self.x1, self.y1 = -30, 150, 20, 200

        for self.row in self.grid:
            for self.column in self.row:
                self.x0 += 50
                self.x1 += 50

                self.cell = self.canvas.create_rectangle(self.x0, self.y0, self.x1, self.y1)
            self.y0 += 50
            self.y1 += 50
            self.x0 = -30
            self.x1 = 20

root = Tk()
root.focus_set()
obj = MainGame()

obj.run()

root.mainloop()

"<Key>" 不是绑定事件的有效键符。尝试一个有效的,如需完整列表,您可以在此处查看 http://www.tcl.tk/man/tcl8.4/TkCmd/keysyms.htm

编辑:看来我错了,不知道这是一个有效的 事件。你每天都能学到东西 :)

稍微修改一下您的代码后,您会发现 tag_bind 仅适用于鼠标点击。在查看了我在 tag_bind 中可以找到的所有文档之后,没有任何内容表明您可以将键绑定到绘制的对象。所以这里的解决方案是将根 window 绑定到键或绑定 canvas 所在的框架。

我已对您的代码进行了一些更改以使其正常工作,但它应该可以帮助您朝着正确的方向前进。如果没有更多关于您在长期 运行 中要完成的工作的信息,很难提供一个好的答案,但我认为这会有所帮助。如果您还有其他问题,请告诉我。

from tkinter import *

class MainGame:
    def __init__(self, parent):
        self.parent = parent
        self.grid = [[""]*15 for n in range(15)]
        self.grid[14][3] = 1
        self.canvas = Canvas(self.parent, width = 900, height = 900)
        self.canvas.pack()        
        self.a, self.b = 45, 175

    #added an argument here so we can better control the player object.
    def noreaction(self, old):
        print("It clicked")
        print(self.a)
        self.a += 50
        self.b += 50
        self.canvas.delete(old)
        # assigned the object to a class attribute
        self.player_obj = self.canvas.create_image(self.a, self.b, image = self.pl, tags = "p2Tag")
        # you will see <Key-q> and <q> work the same here.
        self.parent.bind("<Key-q>", lambda x: self.noreaction(self.player_obj))

    def run(self):
        self.pl = PhotoImage(file = "./Colors/blk.gif")
        # assigned the object to a class attribute
        self.player_obj = self.canvas.create_image(self.a, self.b, image = self.pl, tags = "p1Tag")
        # you will see <Key-q> and <q> work the same here.
        self.parent.bind("<q>", lambda x: self.noreaction(self.player_obj))


        self.x0, self.y0, self.x1, self.y1 = -30, 150, 20, 200
        for self.row in self.grid:
            for self.column in self.row:
                self.x0 += 50
                self.x1 += 50

                self.cell = self.canvas.create_rectangle(self.x0, self.y0, self.x1, self.y1)
            self.y0 += 50
            self.y1 += 50
            self.x0 = -30
            self.x1 + 20

root = Tk()
root.focus_set()
obj = MainGame(root)

obj.run()

root.mainloop()