2017-04-21 1 views
1

두 이미지를 차례대로 이동하려고했지만 두 이미지가 동시에 움직이기 시작합니다. 기본적으로 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

내가 문제를 볼 수 없습니다. 나중에 움직일 때까지 두번째 아이템이 시작되기를 기다리고 싶다면 움직이기 시작할 때까지'self.move_object'를 두 번 호출하지 마십시오. –

답변

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() 
관련 문제