Rhys34512
Rhys34512

Reputation: 39

Image showing up on wrong screen in tkinter

im relatively new to coding and am trying to display an image on tkinter, i have it set so when you press on a button on the first screen it will open to another screen - i have got this working fine my issue is that when the new window is opened the image that should be on the second screen goes onto the first screen, and i cannot work out how to fix it. I dont get any errors it just puts the image in the middle of the first screen, I hope this makes sense. Thanks

The code is below: (for the second screen that should be displaying the image but isnt)

from tkinter import *



window2 = Tk()
window2.geometry("1920x1200")

Namearea = Label(window2, text = "Please Name the Prebuild: ")

Namearea.pack()

e = Entry(window2, width=50, borderwidth=3, bg="Light Grey", fg="black")
e.pack()
e.insert(0, "Remove this text and Enter the name of your prebuild: ")


# this is the part for the image    
img3 = PhotoImage(file=r"C:\Tkinter\ComputerImage.png ")
picture1 = Label(image=img3)
picture1.pack()



SaveAndContinue = Button(window2, text = "Save and Return to Main Menu", padx = 75, pady = 20, bg = "Light Grey")

SaveAndContinue.pack()


LinkTitle = Label(window2, text = "Here are some links to purchase the parts from:")
Link1 = Label(window2, text = "Scorptec: www.scorptec.com.au/")
Link2 = Label(window2, text = "Centre-Com: www.centrecom.com.au/")


LinkTitle.pack()
Link1.pack()
Link2.pack()

  

Upvotes: 0

Views: 419

Answers (2)

acw1668
acw1668

Reputation: 46687

Since you used multiple instances of Tk() (one for first window, one for second window), you need to specify the parent for picture1 and img3:

img3 = PhotoImage(master=window2, file=r"C:\Tkinter\ComputerImage.png")
picture1 = Label(window2, image=img3)
picture1.pack()

However, you should avoid using multiple instances of Tk(). Better change second instance of Tk() to Toplevel().

Upvotes: 1

Ujjwal Dash
Ujjwal Dash

Reputation: 823

Hey you forgot to mention the window name in picture1 = Label(image=img3)

here is the right one the mistake

# this is the part for the image    
img3 = PhotoImage(file=r"C:\\Tkinter\\ComputerImage.png ")
picture1 = Label(window2,image=img3)
picture1.pack()

And the error error _tkinter.TclError: image "pyimage4" doesn't exist - You have to use Toplevel() in second window

window2=Toplevel ()

It works for me

Upvotes: 0

Related Questions