Reputation: 33
I am using Python 2.7 and want to load a .gif logo on the Tkinter frame but there is a problem that it open two windows all the time (one empty and one with logo). Codes:
import Tkinter
root = Toplevel()
logo = PhotoImage(file="D:\\.....\\....\\****.gif")
w1 = Label(root, compound = CENTER, image = logo).pack(side="right")
root.mainloop()
how can I have only one window with my logo?
Upvotes: 1
Views: 1641
Reputation: 7735
Every tkinter app needs a Tk()
window a.k.a root for other widgets to exist. If you don't create it explicitly, it will be created implicitly. Your empty window is that implicitly created Tk()
window and the other one is Toplevel()
you created.
So you need to change this line
root = Toplevel()
to
root = Tk()
Additionally, please keep the reference of your image
.
When you add a PhotoImage or other Image object to a Tkinter widget, you must keep your own reference to the image object. If you don’t, the image won’t always show up.
The problem is that the Tkinter/Tk interface doesn’t handle references to Image objects properly; the Tk widget will hold a reference to the internal object, but Tkinter does not. When Python’s garbage collector discards the Tkinter object, Tkinter tells Tk to release the image. But since the image is in use by a widget, Tk doesn’t destroy it. Not completely. It just blanks the image, making it completely transparent…
Upvotes: 1