我希望当按下按钮时,创建一个命令,使png在几秒钟后消失,然后它出现在另一个位置,然后在几秒钟后出现在另一个位置,然后在另一个位置,就像它在移动一样。我试着用了几个def,睡眠和销毁窗口小部件,但它仍然出现在同一时间,我不知道如何纠正它。我认为可以用"for“来完成,但我还不知道如何使用它。请帮帮我好吗?
def destroy_widget(widget):
widget.destroy()
def texto():
link=tk.StringVar()
no=tk.Label(ventana,text="Hay algo en el camino...",font=("Times",18,"bold"),anchor='n',width=24, height=3,bg="#137420", fg="#9AD51A",bd=25,relief = "ridge")
no.place(x=430,y = 400,anchor ='center')
caja = tk.Entry(ventana,textvariable=link,width=50,state="readonly")
caja.place(x=430,y = 410,anchor ='center')
lin="https://www.tumblr.com/blog/mundo000011"
link.set(lin)
def saliendo():
canvas.delete(ima)
ima2 = canvas.create_image(247,780,image=myimg)
ventana.after(5000, destroy_widget, ima2)
ima3 = canvas.create_image(247,500,image=myimg)
ventana.after(5000, destroy_widget, ima3)
texto()
def salir():
if trad["text"]=="ar.ab.iz.":
trad.config(text=leta)
saliendo()
ventana=tk.Tk()
ventana.title("Bosque")
ventana.geometry('900x800')
ventana.resizable(0,0)
myimg = tk.PhotoImage(file='circlo.png')
posx=70
posy=235
ima = canvas.create_image(78,235,image=myimg)
boton2 = tk.Button(text=" Ir ",font=("Verdana",16), command=salir)
boton2.config(bg="#135C3F", fg="#D7D731")
boton2.place(x=746,y=379)
发布于 2021-06-25 02:33:56
这将对您起作用:
图片是这样的:
初始化主要窗口小部件和变量:
from tkinter import *
import random
from PIL import Image,ImageTk
root=Tk()
width1=900
height1=200
root.geometry(f"{width1}x{height1}+10+10")
random_x=0
random_y=0
然后,我们将创建两个函数:
def create_image(j):
if j!=0: #=== If j is not 0
x=random.randint(0,size1)
y=random.randint(0,size2)
image_lbl.place_forget() #=== Forget the placement of the widget
image_lbl.place(x=x,y=y)
root.after(1000,lambda: create_image(j-1)) #=== Go to the function with value of j as 1 less than the previous
def hello():
j=random.randint(0,10) #=== Random integer from 0 to 10
root.after(1000,lambda: create_image(j)) #=== Lambda is used to pass arguments in the function
最后一部分。它包含图像、宽度和按钮
image=ImageTk.PhotoImage(file=r"path/to/icon.png")
image1=Image.open(r"path/to/icon.png")
width=image1.size[0]
height=image1.size[1]
image_lbl=Label(root,image=image)
size1=width1-width
size2=height1-height
image_lbl.place(x=random_x,y=random_y)
Button(root,text="Change Position",command=hello).pack()
root.mainloop()
完整代码
from tkinter import *
import random
from PIL import Image,ImageTk
root=Tk()
width1=900
height1=200
root.geometry(f"{width1}x{height1}+10+10")
random_x=0
random_y=0
def create_image(j):
if j!=0:
x=random.randint(0,size1)
y=random.randint(0,size2)
image_lbl.place_forget()
image_lbl.place(x=x,y=y)
root.after(1000,lambda: create_image(j-1))
def hello():
j=random.randint(0,10)
print(j)
root.after(1000,lambda: create_image(j))
image=ImageTk.PhotoImage(file=r"path/to/icon.png")
image1=Image.open(r"path/to/icon.png")
width=image1.size[0]
height=image1.size[1]
image_lbl=Label(root,image=image)
size1=width1-width
size2=height1-height
image_lbl.place(x=random_x,y=random_y)
Button(root,text="Change Position",command=hello).pack()
root.mainloop()
https://stackoverflow.com/questions/68124256
复制相似问题