首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >如何让一个图像在一个地方出现,然后在另一个位置出现几次?

如何让一个图像在一个地方出现,然后在另一个位置出现几次?
EN

Stack Overflow用户
提问于 2021-06-25 01:06:00
回答 1查看 38关注 0票数 0

我希望当按下按钮时,创建一个命令,使png在几秒钟后消失,然后它出现在另一个位置,然后在几秒钟后出现在另一个位置,然后在另一个位置,就像它在移动一样。我试着用了几个def,睡眠和销毁窗口小部件,但它仍然出现在同一时间,我不知道如何纠正它。我认为可以用"for“来完成,但我还不知道如何使用它。请帮帮我好吗?

代码语言:javascript
运行
AI代码解释
复制
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)
EN

回答 1

Stack Overflow用户

发布于 2021-06-25 02:33:56

这将对您起作用:

图片是这样的:

初始化主要窗口小部件和变量:

代码语言:javascript
运行
AI代码解释
复制
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

然后,我们将创建两个函数:

代码语言:javascript
运行
AI代码解释
复制
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

最后一部分。它包含图像、宽度和按钮

代码语言:javascript
运行
AI代码解释
复制
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()

完整代码

代码语言:javascript
运行
AI代码解释
复制
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()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68124256

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文