You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Tkinter视频编辑器导入视频后旧缩略图变白问题求助

问题解决:Tkinter导入视频缩略图重复导入后旧图消失

你遇到的问题根源是单个全局变量Tkinter_Bild被重复覆盖:每次导入新视频时,这个变量会指向新的ImageTk.PhotoImage对象,旧的图片对象因为失去了所有引用,会被Python的垃圾回收机制销毁,导致之前的缩略图变成空白。

解决方案

改用全局列表存储所有生成的PhotoImage对象,让每个图片都能保持引用,避免被回收。具体修改如下:

from tkinter import *
from tkinter.filedialog import askopenfilename
from PIL import Image, ImageTk
import cv2

Fenster = Tk()
Fenster.geometry(f"{Fenster.winfo_screenwidth()}x{Fenster.winfo_screenheight()}")
Maximale_Breite = int(Fenster.winfo_screenwidth() * 0.08)
Maximale_Höhe = int(Fenster.winfo_screenheight() * 0.08)
Medien_Importierungen = 0
# 新增:全局列表存储所有图片对象,防止被垃圾回收
Bild_Speicher = []

Medien = Canvas(Fenster, width=Fenster.winfo_screenwidth() * 0.2, height=Fenster.winfo_screenheight(),
                highlightbackground="#dd6800")
Medien.pack(side=LEFT)

Medien_Importieren = Label(Medien, text="Import media")
Medien_Importieren.place(x=int(Fenster.winfo_screenwidth() / 10 - Medien_Importieren.winfo_reqwidth() / 2),
                         y=Fenster.winfo_screenheight() / 20)

def Medien_Hinzufügen(event):
    global Medien_Importierungen, Maximale_Breite, Maximale_Höhe, Bild_Speicher  # 修改:引用列表而非单个图片变量
    Videopfad = askopenfilename(filetypes=[('Video Files', '*.mp4')])
    if Videopfad:
        Geladenes_Video = cv2.VideoCapture(Videopfad)
        Rückgabe, Einzelbild = Geladenes_Video.read()
        if Einzelbild.shape[0] / Maximale_Höhe > Einzelbild.shape[1] / Maximale_Breite:
            height = Maximale_Höhe
            width = int(Einzelbild.shape[1] * height / Einzelbild.shape[0])
        else:
            width = Maximale_Breite
            height = int(Einzelbild.shape[0] * width / Einzelbild.shape[1])
        Einzelbild = cv2.resize(Einzelbild, (width, height))
        Tkinter_Bild = ImageTk.PhotoImage(image=Image.fromarray(cv2.cvtColor(Einzelbild, cv2.COLOR_BGR2RGB)))
        Bild_Speicher.append(Tkinter_Bild)  # 新增:将新图片添加到全局列表
        Thumbnail = Label(Medien, image=Tkinter_Bild)
        if Medien_Importierungen % 2 == 0:
            x_position = Fenster.winfo_screenwidth() * 0.01 + Maximale_Breite / 2 - Einzelbild.shape[1] / 2
        else:
            x_position = Fenster.winfo_screenwidth() * 0.11 + Maximale_Breite / 2 - Einzelbild.shape[1] / 2
        y_position = (Fenster.winfo_screenheight() / 10 +
                        (Fenster.winfo_screenwidth() * 0.01 + Maximale_Höhe) * (Medien_Importierungen // 2) +
                        Maximale_Höhe / 2 - Einzelbild.shape[0] / 2)
        Thumbnail.place(x=x_position, y=y_position)
        Medien_Importierungen += 1

Medien_Importieren.bind("<Button-1>", Medien_Hinzufügen)
mainloop()

原理说明

Tkinter的PhotoImage对象需要持续的引用才能在界面上显示,通过Bild_Speicher列表持有所有图片对象的引用,Python垃圾回收机制就不会销毁这些对象,所有导入的缩略图都能正常保留显示。

内容的提问来源于stack exchange,提问作者Alex

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 01:54:52