2017-04-21 28 views
1

2つの画像を次々と移動しようとしていますが、両方の画像が同時に動き始めます。基本的には、Image1が目的地に到着するまで待ってから、Image2が動くようにします。ここに私のコードは順次キャンバスの項目を移動するにはTkinter画像を次々に移動する

import tkinter as tk 
from PIL import ImageTk 
from PIL import Image 
import time 

class gui(tk.Frame): 

def __init__(self, parent, *args, **kwargs): 
    tk.Frame.__init__(self, parent, *args, **kwargs) 
    self.canvas = tk.Canvas(parent, bg="blue", highlightthickness=0) 
    self.canvas.pack(fill="both", expand=True) 
    self.img = tk.PhotoImage(file="Club14.gif") 
    self.card1 = self.canvas.create_image(0, 0, image=self.img, anchor="nw") 
    self.card2= self.canvas.create_image(800, 800, image=self.img, anchor="nw") 
    self.move_object(self.canvas, self.card1, [400, 410], 8) 
    self.move_object(self.canvas, self.card2, [400, 440], 8) 



def move_object(self, canvas, object_id, destination, speed=50): 
    dest_x, dest_y = destination 
    coords = self.canvas.coords(object_id) 
    current_x = coords[0] 
    current_y = coords[1] 

    new_x, new_y = current_x, current_y 
    delta_x = delta_y = 0 
    if current_x < dest_x: 
     delta_x = 1 
    elif current_x > dest_x: 
     delta_x = -1 

    if current_y < dest_y: 
     delta_y = 1 
    elif current_y > dest_y: 
     delta_y = -1 

    if (delta_x, delta_y) != (0, 0): 
     canvas.move(object_id, delta_x, delta_y) 

    if (new_x, new_y) != (dest_x, dest_y): 
     canvas.after(speed, self.move_object, canvas, object_id, destination, speed) 

if __name__ == "__main__": 
    root = tk.Tk() 
    w, h = root.winfo_screenwidth(), root.winfo_screenheight() 
    root.geometry("%dx%d+0+0" % (w, h)) 
    gui(root) 
    root.mainloop() 
+0

私は問題が表示されません。 2つ目のアイテムが後で移動するのを待つ場合は、 'self.move_object'を移動開始準備が整うまで2度目に呼び出さないでください。 –

答えて

0

である、あなたは以前のものは、そのに達した後にのみ各項目を移動を開始している、リストにそれらを入れて、以下run_move_sequence()などの方法を定義することができます目的地:

animated GIF

import tkinter as tk 

class gui(tk.Frame): 
    def __init__(self, parent): 
     tk.Frame.__init__(self, parent) 
     self.canvas = tk.Canvas(parent, width=200, height=200) 
     self.canvas.pack(fill="both", expand=True) 
     self.objects = [self.canvas.create_oval(10, 10, 30, 30, fill="blue"), 
         self.canvas.create_rectangle(170, 170, 190, 190, fill="yellow") ] 
     self.destinations = [ [90, 75], [90, 100] ] 
     self.speed = 20 
     self.start_move_sequence() 

    def start_move_sequence(self): 
     self.moveDone = False 
     self.count = 0 
     self.run_move_sequence() 

    def run_move_sequence(self): 
     if self.moveDone == False: 
      self.move_object(self.objects[self.count], self.destinations[self.count]) 
      self.canvas.after(self.speed, self.run_move_sequence) 
     else: 
      # Start run_move_sequence on the next object 
      self.count += 1 
      if self.count < len(self.objects): 
       self.moveDone = False 
       self.run_move_sequence() 

    def move_object(self, object_id, destination): 
     dest_x, dest_y = destination 
     coords = self.canvas.coords(object_id) 
     current_x, current_y = coords[0], coords[1] 

     new_x, new_y = current_x, current_y 
     delta_x = delta_y = 0 
     if current_x < dest_x: 
      delta_x = 1 
     elif current_x > dest_x: 
      delta_x = -1 

     if current_y < dest_y: 
      delta_y = 1 
     elif current_y > dest_y: 
      delta_y = -1 

     if (delta_x, delta_y) != (0, 0): 
      self.canvas.move(object_id, delta_x, delta_y) 

     if (new_x, new_y) == (dest_x, dest_y): 
      self.moveDone = True 

if __name__ == "__main__": 
    root = tk.Tk() 
    gui(root) 
    root.mainloop() 
関連する問題