不确定如何在 Tkinter 中设置背景图像



在我开始之前,我知道这里有一篇关于我的问题的帖子, 但是,这并没有完全帮助我,即使在遵循帖子中的答案后,我似乎仍然遇到问题。

我似乎得到了两个不同的"错误"

一个错误,它显示图像文件不可读。 这是在使用此代码时给出的(这是从另一篇文章中给出的作为有效答案的(:

self.background_image=tk.PhotoImage…
self.background_label = tk.Label(parent, image=background_image)
self.background_label.place(x=0, y=0, relwidth=1, relheight=1)

另一个"错误",当使用以下代码时,没有显示图像,但没有给出真正的错误消息:

self.background_image = tk.PhotoImage(r'C:/hazuki-gui/resources/background1.png')
self.background_label = tk.Label(image=self.background_image)
self.background_label.place(x=0,y=0)

我环顾四周,一切都只是显示了我上面展示的第一种方式。 我尝试使用pngjpg图像,但在两种情况下都返回相同的结果。

任何有关这方面的帮助将不胜感激。

旁注:如果如何为 python 2.7 和 python 3.x 执行此操作有任何差异,请告诉我(目前我正在使用 python 2.7,但会将其移至 3.x(

也许您的图像被垃圾回收,因为没有引用您的图像?

self.background_image = tk.PhotoImage(file=r'C:/hazuki-gui/resources/background1.png')
self.background_label = tk.Label(image=self.background_image)
self.background_label.place(x=0,y=0)
self.background_label.img = self.background_image #try to add this

最新更新