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

Tkinter/Python启动闪屏报错:TclError: pyimage38不存在

Tkinter闪屏报错‘TclError: "pyimage38" doesn't exist’排查与修复

问题原因

报错核心是图片引用生命周期异常+窗口显示时机错误:

  • 你在调用闪屏加载函数后立刻执行root.deiconify(),导致主窗口提前渲染,此时闪屏标签的图片还未完成绑定,Tkinter无法定位到对应的图片对象。
  • 虽然代码给标签绑定了image属性,但窗口提前显示的逻辑打乱了图片对象的持有周期,触发了垃圾回收。

修复后的代码

import tkinter as tk
from tkinter import ttk
from PIL import ImageTk, Image
import os

current_directory = os.getcwd()
print(current_directory)

def load_splash_image():
    # 加载闪屏图片
    splash_path = os.path.join(current_directory, "splash.png")
    splash_image = Image.open(splash_path)
    splash_tk_image = ImageTk.PhotoImage(splash_image)
    
    # 创建显示图片的标签
    splash_label = ttk.Label(root)
    splash_label.config(image=splash_tk_image)
    splash_label.image = splash_tk_image  # 持有图片引用,防止被垃圾回收
    
    # 居中显示闪屏(添加anchor确保真正居中)
    splash_label.place(relx=0.5, rely=0.5, anchor="center")
    
    # 3秒后销毁闪屏并显示主窗口
    root.after(3000, lambda: [splash_label.destroy(), root.deiconify()])

# 创建主窗口并先隐藏
root = tk.Tk()
root.withdraw()

# 加载并显示闪屏
load_splash_image()

root.mainloop()

关键修改点

  • 将root.deiconify()移到定时器回调中,确保主窗口在闪屏销毁后才显示,避免图片绑定未完成时窗口提前渲染。
  • 给place方法添加anchor="center"参数,修正原代码中闪屏未真正居中的问题。
  • 移除不必要的全局变量splash_tk_image,标签的image属性已足够持有图片引用,防止垃圾回收。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 09:47:42