我正在使用python开发的一个小程序旨在按一定顺序显示一系列图像和音频片段。我可以在同一窗口中显示两个图像,一个可以按预期覆盖另一个图像,但是我正在寻找一种方法,可以使第二个图像在一定时间(30秒)后而不是尽快显示在程序运行时。不过,我似乎无法使第二张名为mutableMapOf()
的图像与程序的其余部分分开运行。
要尝试解决此问题,我加入了textBox.jpg
函数,但这只是延迟了整个代码的启动。
sleep()
我希望在程序运行了大约30秒并且已经显示了另一个图像之后调用第二个图像from tkinter import *
from tkinter import ttk
from PIL import Image, ImageTk
import time
# Title/Main
window = Tk()
window.title("Just Monika")
window.configure(background="black")
# control window size to make full-screen
width_value=window.winfo_screenwidth()
height_value=window.winfo_screenheight()
window.geometry("%dx%d+0+0" % (width_value, height_value))
# Monika photo
# this block will allow the image to resize to fit the window's size
# indefinitely
def resize_image(event):
new_width = event.width
new_height = event.height
image = copy_of_image.resize((new_width, new_height))
photo = ImageTk.PhotoImage(image)
label.config(image = photo)
label.image = photo #avoid garbage collection
monika = Image.open("C:\\Python Programs\\Just Monika\\Images\\justMonika.png")
copy_of_image = monika.copy()
photo = ImageTk.PhotoImage(monika)
label = ttk.Label(window, image = photo)
label.bind('<Configure>', resize_image)
label.pack(fill=BOTH, expand=YES)
# text box image
textBox = Image.open("C:\\Python Programs\\Just Monika\\Images\\textBox.jpg")
tkTextBox = ImageTk.PhotoImage(textBox)
panel1 = Label(window, image = tkTextBox)
panel1.place(x=675, y=300)
time.sleep(30)
。
答案 0 :(得分:0)
按照@Masoud的建议,您可以使用通用窗口小部件after()
方法来安排在指定的时间延迟后调用的函数:
from tkinter import *
from tkinter import ttk
from PIL import Image, ImageTk
PATH_IMAGE1 = 'justMonika.png'
PATH_IMAGE2 = 'textBox.png'
DELAY = 5000 # In milliseconds
# Title/Main
window = Tk()
window.title("Just Monika")
window.configure(background="black")
# control window size to make full-screen
width_value=window.winfo_screenwidth()
height_value=window.winfo_screenheight()
window.geometry("%dx%d+0+0" % (width_value, height_value))
# Monika photo
# this block will allow the image to resize to fit the window's size
# indefinitely
def resize_image(event):
new_width = event.width
new_height = event.height
image = copy_of_image.resize((new_width, new_height))
photo = ImageTk.PhotoImage(image)
label.config(image = photo)
label.image = photo #avoid garbage collection
def display_2nd_image():
global panel1
# text box image
textBox = Image.open(PATH_IMAGE2)
tkTextBox = ImageTk.PhotoImage(textBox)
panel1 = Label(window, image=tkTextBox)
panel1.place(x=675, y=300)
panel1.image = tkTextBox #avoid garbage collection
monika = Image.open(PATH_IMAGE1)
copy_of_image = monika.copy()
photo = ImageTk.PhotoImage(monika)
label = ttk.Label(window, image=photo)
label.bind('<Configure>', resize_image)
label.pack(fill=BOTH, expand=YES)
window.after(DELAY, display_2nd_image)
window.mainloop()