Canvas.move无法在Python中工作

时间:2015-04-07 19:47:30

标签: python tkinter

所以我正在为一项任务做这个基本游戏,其核心是用箭头键在画布周围移动一个圆圈。由于我无法确定的原因,圆圈不会移动,似乎甚至没有调用移动功能。我环顾四周,发现一些人有类似的问题,但我尝试修复他们的无济于事。任何帮助将不胜感激。

    from tkinter import *
    class CircleGame(Frame):
  def __init__(self):
    Frame.__init__(self)
    self.master.title("Circle Game")
    self.grid()

    self.canvas = Canvas(self, width = 300, height = 300, bg = "white")
    self.canvas.grid(row = 0, column = 0)

    self.canvas.create_oval(100,100,200,200,tags = "circle")
    self.x = 150
    self.y = 150

    self.canvas.bind("<Up>", self.moveCircle)
    self.canvas.bind("<Down>", self.moveCircle)
    self.canvas.bind("<Left>", self.moveCircle)
    self.canvas.bind("<Right>", self.moveCircle)


  def moveCircle(self, event):
    if event.keysym == "Up":
        self.canvas.move("circle", 0, -5)
        self.y -= 5

    elif event.keysym == "Down":
        self.canvas.move("circle", 0, 5)
        self.y +=5

    elif event.keysym == "Left":
        self.canvas.move("circle", -5, 0)
        self.x -=5    

    elif event.keysym == "Right":
        self.canvas.move("circle", 5, 0)
        self.x +=5      
    self.canvas.update()


def main():
    CircleGame().mainloop()

main()     

1 个答案:

答案 0 :(得分:1)

好的问题很简单,我没有添加一个self.canvas.focus(),因此窗口没有接收到我的输入