0

Images are working on my root window, though when i try the same code to put an image on the new top window, the image does not show. Is there a way to put images on a Toplevel window or can you only place them on the root window?

This code shows the output when the UI i want is placed ontop a Toplevel window:

from tkinter import *

def next2():
    window2 = Toplevel(window)
    window2.grab_set()
    
    window2.geometry("800x600")
    window2.resizable(False,False)
    
    message = "Insert message here."
    messagelength = len(message)

    C = Canvas(window2, height=50, width=50)
        
    filename = PhotoImage(file = "cardTemplate3.png")
    background_label = Label(window2, image=filename, compound = "center", bg = "#ffe9ec")
    background_label.place(x=0, y=10, relwidth=1, relheight=1)
    C.pack()

    label = Label(window2, text = messagefull, background = "white")
    label.place(x=265, y= 260)

    step3Lbl = Label(window2, text = "Step 3: Preview", background = "#FFC0CB", fg = "white", padx = 205, pady = 20, font = ("Arial Bold", 40))
    step3Lbl.place(x=0, y=0)
    
window = Tk()

window.geometry("800x600")
    
background_image=PhotoImage(file = "heartBg.png")
background_label = Label(window, image=background_image)
background_label.place(x=0, y=0, relwidth=1, relheight=1)

headerLbl = Label(window, text = "Image works here", background = "#FFC0CB", fg = "white", padx = 300, pady = 25, font = ("Arial Bold", 40))
headerLbl.pack()

strtBtn = Button(window, text = "Start button", pady = 17, bg = 'white', width = 30, font = 13, command = next2)
strtBtn.pack()

window.mainloop()

Below shows the same code if it were on a root window, and it displays perfectly:

from tkinter import *

window4 = Tk()
window4.geometry("800x600")
window4.config(background = "#E9ADB8")
window4.iconbitmap(r"AHSlogo.ico")

message = "Insert message here."
messagelength = len(message)

line1 = message[0:50]
line2 = message[51:99]
line3 = message[100:149]
line4 = message[150:200]


messagefull = line1 + "\n" + line2 + "\n" + line3  + "\n" + line4

C = Canvas(window4, height=50, width=50)
    
filename = PhotoImage(file = "cardTemplate3.png")
background_label = Label(window4, image=filename, compound = "center", bg = "#ffe9ec")
background_label.place(x=0, y=10, relwidth=1, relheight=1)
C.pack()

label = Label(window4, text = messagefull, background = "white")
label.place(x=265, y= 260)

step3Lbl = Label(window4, text = "Step 3: Preview", background = "#FFC0CB", fg = "white", padx = 205, pady = 20, font = ("Arial Bold", 40))
step3Lbl.place(x=0, y=0)

window4.mainloop()
nikki
  • 3
  • 3

0 Answers0