如何打开另一个GUI窗口,一旦视频在GUI中完成在python tkinter?



下面代码的目的是在视频完成后自动打开GUI的另一个窗口(视频长度为10秒)

我试过这样做,但有些不工作,请看看下面的代码。

import tkinter as tk, threading
import imageio
import tkinter as tk
from tkinter import *
from tkinter import ttk
import tkinter.font as font
import os
import re
from PIL import Image, ImageTk
global Image
global image
global Label
video_name = "C:\Users\Ray\Desktop\Duplicate.mp4" 
video = imageio.get_reader(video_name)
def stream(label):
for image in video.iter_data():
frame_image = ImageTk.PhotoImage(Image.fromarray(image))
label.config(image=frame_image)
label.image = frame_image
if __name__ == "__main__":
root = tk.Tk()
my_label = tk.Label(root)
my_label.pack()
thread = threading.Thread(target=stream, args=(my_label,))
thread.daemon = 1
thread.start()
ima = Image.open('C:\Users\Ray\Desktop\July_bill.jpg')
ima = ima.resize((1920,1050), Image.ANTIALIAS)
my_img = ImageTk.PhotoImage(ima)
my_lbl = Label(Image = my_img)
my_lbl.pack()
root.mainloop()

在这段代码中,如果我删除图像插入代码(这是代码中的ima, my_img行),视频播放顺利,但如果我使用这段代码,那么它不工作,它显示的错误"未知选项"-Image">

谁能帮我解决这个问题?那就太好了。

问候,雷

my_lbl = Label(Image = my_img)改为my_lbl = Label(image = my_img)

您可以在这里看到TK标签选项。正如你看到的"图像"以小写字母开头,在你的代码中,你有&;image&;导致错误。

完整代码:

import tkinter as tk, threading
import imageio
from tkinter import *
from PIL import Image, ImageTk
from time import sleep
def stream(label):
video_name = "C:\Users\Ray\Desktop\Duplicate.mp4" 
video = imageio.get_reader(video_name)
for image in video.iter_data():
frame_image = ImageTk.PhotoImage(Image.fromarray(image))
label.config(image=frame_image)
label.image = frame_image
if __name__ == "__main__":
root = tk.Tk()
my_label = tk.Label(root)
my_label.pack()
thread = threading.Thread(target=stream, args=(my_label,))
thread.daemon = 1
thread.start()
sleep(10)
ima = Image.open('C:\Users\Ray\Desktop\July_bill.jpg')
ima = ima.resize((1920,1050), Image.ANTIALIAS)
my_img = ImageTk.PhotoImage(ima)
my_lbl = Label(image = my_img)
my_lbl.pack()
root.mainloop()

我没有测试视频,但图像效果很好。

最新更新