python-3.xtkinterphotoimage

Two image in tkinter using PhotoImage


I'm making a program that is using 2 different images. I have loading page which is just a picture called 'loading.png' which works fine.

When I try and add a different image called 'table.png', using the same method, onto a different window it doesn't display but on the intro window it displays the image I want.

This is the loading screen code

win=Tk()
win.title('PokerChamp')
win.geometry('400x200')

background_image = PhotoImage(file='loading.png')
background_label = Label(win, image=background_image)
background_label.place(x=0, y=0, relwidth=1, relheight=1)

This works fine but when I add this code it displays the wrong image and the main window's image doesn't show.

root=Tk()
root.withdraw()
root.config(bg='#1b800b')
root.title('PokerChamp')

image = PhotoImage(file='table.ppm')
label = Label(win, image=image)
label.place(x=0, y=0)

Any Ideas?


Solution

  • First of all, like @AD WAN said, it is incorrect to have to instances of Tk(). Second of all, when you initiate the second image, you put it in the first one, win, instead of root.

    This would be your code:

    from tkinter import *
    win=Tk()
    win.title('PokerChamp')
    win.geometry('400x200')
    
    background_image = PhotoImage(file='loading.png')
    background_label = Label(win, image=background_image)
    background_label.place(x=0, y=0, relwidth=1, relheight=1)
    
    root=Toplevel()
    
    #root.config(bg='#1b800b')
    root.title('PokerChamp')
    
    image = PhotoImage(file='table.ppm')
    label = Label(root, image=image)
    label.place(relx=0, rely=0)
    

    Hope this helps!