2017-04-03 41 views
0

我有一個圈子,我想移動一張地圖,我處於早期階段,但我已經看了很多地方,並不能找出爲什麼圓不是移動。我的代碼是canvas.move不移動python

import Tkinter as Tkinter 

class gameScreen: 
    def moveup(self, event): 
     self.canvas.move(self.char, -100, 0) 
     self.canvas.focus(self.char) 
     self.canvas.update() 

    def __init__(self, master): 

     self.master = master 
     master.title("Game") 

     master.resizable(width=False, height=False) 
     self.img = tkinter.PhotoImage(file = "platformer.gif") 
     self.canvas = tkinter.Canvas(master, width=self.img.width(), 
              height=self.img.height()) 

     self.canvas.pack(expand="YES",fill="both") 

     self.canvas.create_image(0, 0,anchor="nw", image = self.img) 
     self.char = tkinter.PhotoImage(file = "hero.gif") 
     self.canvas.create_oval(0, 0, 50, 50, fill="red") 
     self.x = 0 
     self.y = 0 
     master.bind("<Up>", self.moveup)   

root = tkinter.Tk() 
my_gui = gameScreen(root) 
root.mainloop()` 

回答

1

你得給move方法的ID或標記爲畫布上的對象。創建畫布項目時會返回該ID。

class gameScreen: 
    def moveup(self, event): 
     ... 
     self.canvas.move(self.canvas_item, -100, 0) 
     ... 

    def __init__(self, master): 
     ... 
     self.canvas_item = self.canvas.create_oval(...) 
     ...