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