简体   繁体   中英

Image on Tkinter button is not showing

I don't know why the image not showing.
Is this a device problem or some update in the Tkinter library?

from tkinter import *
root = Tk()
btn = Button(master=root,image=PhotoImage(file='Sample1.png'))
btn.pack()


root.mainloop()

Sample Image:

在此处输入图像描述

在此处输入图像描述

When a PhotoImage object is garbage-collected by Python (eg when you return from a function which stored an image in a local variable), the image is cleared even if it's being displayed by a Tkinter widget.

Python garbage-collects any local objects at the end of the scope, even when being used by a Tkinter widget. To prevent this, you need to assign the image to some variable:

photoimage = PhotoImage(file='Sample1.png')
btn = Button(master=root,image=photoimage)

Related: Tkinter vanishing PhotoImage issue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM