如何修复tkinter中的键绑定

时间:2017-08-30 16:08:14

标签: python python-3.x tkinter 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()

2 个答案:

答案 0 :(得分:0)

稍微弄乱你的代码之后,tag_bind只能点击鼠标才能找到它。在查看了我在tag_bind中找到的所有文档后,没有任何内容表明您可以将键绑定到绘制的对象。所以这里的解决方案是将根窗口绑定到键或绑定画布放置的帧。

我已经改变了你的代码以使其工作,但它应该帮助你朝着正确的方向前进。如果没有更多关于你想要实现什么的信息,从长远来看很难提供一个好的答案,但我认为这将有所帮助。如果您还有其他问题,请与我们联系。

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()

答案 1 :(得分:-2)

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

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