Python Tkinter 界面添加背景图片报错:TclError: image "pyimage10" doesn't exist
在 Python Tkinter 界面中使用 grid 布局添加背景图片时,可能会遇到 'TclError: image 'pyimage10' doesn't exist' 的错误。
这个错误通常是由于图像对象被垃圾回收导致的。为了解决这个问题,你需要将图像对象存储为实例变量或全局变量。
你可以使用以下代码来存储图像对象:
bg_img = ImageTk.PhotoImage(Image.open('数据科学.png'))
bg_label = tk.Label(root, image=bg_img)
bg_label.image = bg_img # 存储图像对象
bg_label.grid(row=0, column=0, sticky='nsew')
这样,图像对象就会被存储在 bg_label 实例中,而不会被垃圾回收。
原文地址: https://www.cveoy.top/t/topic/ovnB 著作权归作者所有。请勿转载和采集!