Not Able to See the Picture in Tkinter Python Program
Question:
topper = Toplevel()
topper.title("2nd Window")
topper.state('zoomed')
my_img = ImageTk.PhotoImage(Image.open("diamond.png"))
my_label = Label(topper, image=my_img, height = 100 , width = 100)
F21 = Frame(topper, borderwidth=83, bg="blue", relief=SUNKEN)
button1 = Button(topper, text="class", command=topper.destroy)
button1.pack()
my_label.pack()
I am running the code and I get no errors, button is working as well but I’m not able to see the picture in the window.
Answers:
Welcome to Stackoverflow Shakti!
For the future – it is always good to supply a Minimal, Reproducible Example so others can replicate and understand your issue better in order to help you! It also helps you to understand where exactly the error originates.
When you call Toplevel() from another tkinter window to open a new window, you will need to call “mainloop()” on the second window as well in order to display an image – try my code with an example image and comment/uncomment the line with
topper.mainloop()
to see the difference in functionality.
The adapted code:
from tkinter import *
from PIL import Image, ImageTk # pip install pillow
def show_second_window():
topper = Toplevel()
topper.title("2nd Window")
topper.state('zoomed')
my_img = ImageTk.PhotoImage(Image.open("t1.png"))
my_label = Label(topper, image=my_img, height=100, width=100)
F21 = Frame(topper, borderwidth=83, bg="blue", relief=SUNKEN)
button1 = Button(topper, text="class", command=topper.destroy)
button1.pack()
my_label.pack()
topper.mainloop() # <---- this is needed to show the image!
root = Tk()
root.title("1st window")
button = Button(root, text="show second window", command=show_second_window)
button.pack()
root.mainloop()
topper = Toplevel()
topper.title("2nd Window")
topper.state('zoomed')
my_img = ImageTk.PhotoImage(Image.open("diamond.png"))
my_label = Label(topper, image=my_img, height = 100 , width = 100)
F21 = Frame(topper, borderwidth=83, bg="blue", relief=SUNKEN)
button1 = Button(topper, text="class", command=topper.destroy)
button1.pack()
my_label.pack()
I am running the code and I get no errors, button is working as well but I’m not able to see the picture in the window.
Welcome to Stackoverflow Shakti!
For the future – it is always good to supply a Minimal, Reproducible Example so others can replicate and understand your issue better in order to help you! It also helps you to understand where exactly the error originates.
When you call Toplevel() from another tkinter window to open a new window, you will need to call “mainloop()” on the second window as well in order to display an image – try my code with an example image and comment/uncomment the line with
topper.mainloop()
to see the difference in functionality.
The adapted code:
from tkinter import *
from PIL import Image, ImageTk # pip install pillow
def show_second_window():
topper = Toplevel()
topper.title("2nd Window")
topper.state('zoomed')
my_img = ImageTk.PhotoImage(Image.open("t1.png"))
my_label = Label(topper, image=my_img, height=100, width=100)
F21 = Frame(topper, borderwidth=83, bg="blue", relief=SUNKEN)
button1 = Button(topper, text="class", command=topper.destroy)
button1.pack()
my_label.pack()
topper.mainloop() # <---- this is needed to show the image!
root = Tk()
root.title("1st window")
button = Button(root, text="show second window", command=show_second_window)
button.pack()
root.mainloop()