import tkinter
from tkinter import *
from PIL import ImageTk, Image
root=Tk()
root.title("To-Do-List")
root.geometry("400x600+400+100")
root.resizable(width=False, height=False)
task_list=[]
image1=ImageTk.PhotoImage(Image.open("image/top.png"))
root.iconbitmap("image/tick.png")
imagelabel=Label(root,image=image1)
infolabel=Label(root, text="To-Do-List",font="Helvetica 12 bold")
imagelabel.pack()
infolabel.pack()
root.mainloop()
this window appears when i press run, it should have an icon on top and the picture according to the code, but none of these appear to be here
The image files are addressed correctly, but I am getting a blank screen. What can I do?
the link to two png files
I checked if PIL was installed, and yes, it was. I am expecting the picture to appear. I also checked if the pictures are there using the os
module, and it showed true.
import tkinter
from tkinter import *
from PIL import ImageTk, Image
root=Tk()
root.title("To-Do-List")
root.geometry("400x600+400+100")
root.resizable(width=False, height=False)
task_list=[]
image1=ImageTk.PhotoImage(Image.open("image/top.png"))
root.iconbitmap("image/tick.png")
imagelabel=Label(root,image=image1)
infolabel=Label(root, text="To-Do-List",font="Helvetica 12 bold")
imagelabel.pack()
infolabel.pack()
root.mainloop()
this window appears when i press run, it should have an icon on top and the picture according to the code, but none of these appear to be here
The image files are addressed correctly, but I am getting a blank screen. What can I do?
the link to two png files https://drive.google.com/drive/folders/1d1-BxQfZ_WdTHuktgnVjBl_yVEOvzktF?usp=sharing
I checked if PIL was installed, and yes, it was. I am expecting the picture to appear. I also checked if the pictures are there using the os
module, and it showed true.
3 Answers
Reset to default 1The problem is that you need to resize top.png
. The Image has dimensions of 1080x1920, so the Label area that contains it is also 1080x1920, and it is taking up the entire 400x600 window.
It's not entirely clear from the docs what happens if the parcel is larger than the entire window, but I'm guessing it is anchored in center by default, so the pink visible part of the image is above the top of the window, and the window is filled with the 0-alpha value portion of the image, which means you get the bg color of the label. The To-Do-List
info-label is not getting packed at all as there is no space left in the window.
You can see the sizing issue if you change from using pack to imagelabel.place(x=0, y=0, anchor=NW)
. I added cyan background to the label here as part of my debugging.
Either resize your source image, or you'll have to resize it after loading it with PIL. I also had to change imagelabel.pack()
to imagelabel.place(x=0, y=0)
, otherwise the infolabel was not visible since it would get packed below the bottom of the window. If you changed top.png to be just the pink area and 400xY and still used pack(), then the infolabel would appear below the pink area instead of layered on top of it.
import tkinter
from tkinter import *
from PIL import ImageTk, Image
root=Tk()
root.title("To-Do-List")
root.geometry("400x600+400+100")
root.resizable(width=False, height=False)
task_list=[]
image1=ImageTk.PhotoImage(Image.open("image/top.png").resize((400,600)))
root.iconbitmap("image/tick.png")
imagelabel=Label(root,image=image1)
infolabel=Label(root, text="To-Do-List",font="Helvetica 12 bold")
imagelabel.place(x=0, y=0)
infolabel.pack()
root.mainloop()
Your script is incompatible with Python 3.12.8. I still have an empty root.iconbitmap
.
Avoid using this from tkinter import *
Why utilize both namespace import tkinter
and from tkinter import *
?
The problem can be fixed.
Include root.tk.call
in place of iconbitmap
.
Here is re-wrote script:
import tkinter as tk
from PIL import ImageTk, Image
root = tk.Tk()
root.title("To-Do-List")
root.geometry("400x600+400+100")
root.resizable(width=False, height=False)
#root.tk.call("wm", "iconphoto", root._w, tk.PhotoImage(file="p2.png"))
task_list=[]
image1=ImageTk.PhotoImage(Image.open("p2.png").resize((400,600)))
root.tk.call("wm", "iconphoto", root._w, tk.PhotoImage(file="p2.png"))
imagelabel=tk.Label(root,image=image1)
infolabel=tk.Label(root, text="To-Do-List",font="Helvetica 12 bold")
imagelabel.place(x=0, y=0)
infolabel.pack()
root.mainloop()
Screenshot(you can see iconbitmap):
This is caused by Python's garbage collection routine deleting the image, since you aren't using image1
after creating the Label
Python assumes that you don't need it and thus deletes it; in order to fix the issue you have to store the reference like so
image1 = ImageTk.PhotoImage(Image.open("image/top.png"))
imagelabel = Label(root,image=image1)
imagelabel.image = image1
.png
or.bmp
images for the icon; although even in that case the main image still appears. Try using a proper.ico
image in place of tick.png. – OldBoy Commented Feb 5 at 12:31