Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x Tkinter一个接一个地移动图像_Python 3.x_Tkinter - Fatal编程技术网

Python 3.x Tkinter一个接一个地移动图像

Python 3.x Tkinter一个接一个地移动图像,python-3.x,tkinter,Python 3.x,Tkinter,我试图一个接一个地移动两个图像,但两个图像同时开始移动。基本上,我想要的是等到Image1到达目的地,然后Image2开始移动。这是我的密码 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, pare

我试图一个接一个地移动两个图像,但两个图像同时开始移动。基本上,我想要的是等到Image1到达目的地,然后Image2开始移动。这是我的密码

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()
将tkinter作为tk导入
从PIL导入ImageTk
从PIL导入图像
导入时间
类gui(tk.Frame):
定义初始化(自、父、*args、**kwargs):
tk.Frame.\uuuuu init\uuuuuu(self,parent,*args,**kwargs)
self.canvas=tk.canvas(父级,bg=“蓝色”,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(800800,image=self.img,anchor=“nw”)
self.move_对象(self.canvas,self.card1[400410],8)
self.move_对象(self.canvas,self.card2[400440],8)
def move_对象(自身、画布、对象id、目标、速度=50):
目的地x,目的地y=目的地
coords=self.canvas.coords(对象id)
当前_x=坐标[0]
当前_y=坐标[1]
新建x,新建y=当前x,当前y
δx=δy=0
如果当前值小于目标值:
delta_x=1
elif current_x>dest_x:
delta_x=-1
如果当前日期<目的日期:
δy=1
如果当前>目标:
delta_y=-1
如果(δx,δy)!=(0, 0):
canvas.move(对象id、增量x、增量y)
如果(新的x,新的y)!=(目的地x,目的地y):
canvas.after(速度,self.move\u对象,画布,对象id,目的地,速度)
如果名称=“\uuuuu main\uuuuuuuu”:
root=tk.tk()
w、 h=root.winfo_屏幕宽度(),root.winfo_屏幕高度()
根几何体(“%dx%d+0+0”%(w,h))
gui(根目录)
root.mainloop()

要按顺序移动画布中的项目,可以将它们放入列表中,并定义一种方法,如下面的
运行移动顺序()
,该方法仅在前一个项目到达其目标后才开始移动每个项目:

将tkinter作为tk导入
类gui(tk.Frame):
定义初始化(自身,父级):
tk.Frame.\uuuu init\uuuuu(自,父)
self.canvas=tk.canvas(父级,宽度=200,高度=200)
self.canvas.pack(fill=“both”,expand=True)
self.objects=[self.canvas.create_oval(10,10,30,30,fill=“blue”),
self.canvas.create_矩形(170170190190,fill=“yellow”)]
self.destinations=[[90,75],[90,100]]
自身速度=20
self.start\u move\u sequence()
def启动\移动\顺序(自):
self.moveDone=False
self.count=0
self.run\u move\u sequence()
def运行移动顺序(自):
如果self.moveDone==False:
self.move\u对象(self.objects[self.count],self.destinations[self.count])
self.canvas.after(self.speed、self.run\u move\u sequence)
其他:
#在下一个对象上开始运行\u移动\u序列
self.count+=1
如果self.countdest_x:
delta_x=-1
如果当前日期<目的日期:
δy=1
如果当前>目标:
delta_y=-1
如果(δx,δy)!=(0, 0):
self.canvas.move(对象id、增量x、增量y)
如果(new_x,new_y)=(dest_x,dest_y):
self.moveDone=True
如果名称=“\uuuuu main\uuuuuuuu”:
root=tk.tk()
gui(根目录)
root.mainloop()

要按顺序移动画布中的项目,可以将它们放入列表中,并定义一种方法,如下面的
运行移动顺序()
,该方法仅在前一个项目到达其目标后才开始移动每个项目:

将tkinter作为tk导入
类gui(tk.Frame):
定义初始化(自身,父级):
tk.Frame.\uuuu init\uuuuu(自,父)
self.canvas=tk.canvas(父级,宽度=200,高度=200)
self.canvas.pack(fill=“both”,expand=True)
self.objects=[self.canvas.create_oval(10,10,30,30,fill=“blue”),
self.canvas.create_矩形(170170190190,fill=“yellow”)]
self.destinations=[[90,75],[90,100]]
自身速度=20
self.start\u move\u sequence()
def启动\移动\顺序(自):
self.moveDone=False
self.count=0
self.run\u move\u sequence()
def运行移动顺序(自):
如果self.moveDone==False:
self.move\u对象(self.objects[self.count],self.destinations[self.count])
self.canvas.after(self.speed、self.run\u move\u sequence)
其他:
#在下一个对象上开始运行\u移动\u序列
self.count+=1
如果self.countdest_x:
delta_x=-1
如果当前日期<目的日期:
δy=1
如果当前>目标:
delta_y=-1
如果(δx,δy)!=(0, 0):
self.canvas.move(对象id、增量x、增量y)
如果(new_x,new_y)=(dest_x,dest_y):
self.moveDone=True
如果名称=“\uuuuu main\uuuuuuuu”:
root=tk.tk()
gui(根目录)
root.mainloop()

我看不出有什么问题。如果您想等待启动s
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()