Я хочу, чтобы мои изображения tkinter появлялись каждые 4 секунды, а не все сразу - PullRequest
0 голосов
/ 19 марта 2020

Эй, я хочу, чтобы одно и то же изображение выскочило на 4 секунды в другом месте. В этот момент все фотографии сразу появляются. Я пытался добавить time.sleep для for l oop, но все фотографии всплывают сразу любым способом. Пожалуйста, попробуйте мой код и фактически покажите мне, как исправить, так как я очень плохо знаком с python. Спасибо

from tkinter import *
from PIL import ImageTk, Image
from os import walk, getenv, system
from shutil import copyfile
import subprocess
import requests, subprocess, os, tempfile
import os
import tkinter.messagebox
import time


# This is the fuction that makes the original photo and is what root.after calls to later on in the code. 
def ShowAnotherWin(i):
    win = Toplevel()
    image = ImageTk.PhotoImage(Image.open('C:/Users/capture.PNG'))
    win.geometry(i)
    canvas =Canvas(win, width=420, height=560)
    canvas.create_image(0, 0, image= image, anchor=NW)
    canvas.pack()
    win.overrideredirect(1)
    win.mainloop() 




# This is the list it, it consists of different y and x interpects point and will be combined with the showanotherwin to makee a unique image pop up.

YourImageList = ['420x544+0+0',  
  time.sleep(1)
 '420x544+200+600',
  time.sleep(88)
 '420x544+1300+100',
 '420x544+1500+800',
 '420x544+900+500',
 '420x544+500+75',



]

# this code right here takes all the elements in the list and combines it with the showanotherwin. It then displays on screen.
root = Tk()
for i in YourImageList:
    root.after(0, lambda i=i: ShowAnotherWin(i))
    # time.sleep(1)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> leads to all the photos poping up with the only, effect being the duration of the photos poping up. Please help!!

# this right here is just making another photo by itself dont worry about it.   
image = ImageTk.PhotoImage(Image.open('C:/Users/capture.PNG'))
root.geometry('420x544+750+200')
canvas = Canvas(root, width=420, height=560)
canvas.create_image(0, 0, image= image, anchor=NW)
canvas.pack()
root.overrideredirect(1)
root.mainloop()



Ответы [ 2 ]

0 голосов
/ 19 марта 2020

Простой способ:

for i, x in enumerate(YourImageList):
    root.after(i*4000, lambda x=x: ShowAnotherWin(x))

Другой способ - вызов after() внутри ShowAnotherWin():

def ShowAnotherWin(i=0):
    if i < len(YourImageList):
        win = Toplevel()
        image = ImageTk.PhotoImage(file='C:/Users/capture.PNG')
        win.geometry(YourImageList[i])
        canvas =Canvas(win, width=420, height=560)
        canvas.create_image(0, 0, image= image, anchor=NW)
        canvas.pack()
        canvas.image = image # need to keep a reference to image
        win.overrideredirect(1)
        #win.mainloop()  # no need to call mainloop() here
        root.after(4000, ShowAnotherWin, i+1)

...

ShowAnotherWin()
0 голосов
/ 19 марта 2020

Вы, по сути, вооружаете их всех, чтобы они появились здесь с задержкой 0 миллисекунд root.after( 0 ,...

for i in YourImageList:
    root.after(0, lambda i=i: ShowAnotherWin(i))
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...