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

Python Tkinter中图片无法显示问题求助

解决Tkinter图片无法显示的问题

问题核心是PhotoImage对象被Python垃圾回收机制销毁:你直接在Label参数里创建ImageTk.PhotoImage,这个临时对象没有被额外变量保存引用,程序运行后会被自动回收,导致Label无法加载图片。

修改后的代码

from tkinter import *
from PIL import Image, ImageTk

root = Tk()
root.geometry("1000x700")
root.minsize(1000, 700)
root.maxsize(1000, 700)

# 先创建图片对象并保存引用
img = ImageTk.PhotoImage(Image.open("image_processing20200410-19194-aihwb4.png"))
bg_img = Label(image=img, compound=CENTER)
# 将图片对象绑定到Label实例,进一步防止被回收
bg_img.image = img
bg_img.pack(expand=1, fill=BOTH)

root.mainloop()

额外检查项

  • 确认图片路径正确:要么和脚本在同一目录,要么使用绝对路径
  • 确认PIL库已正确安装:执行pip install pillow可完成安装或更新

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 17:10:20