I ran into a probleme when coding a bigger project (at least for me as an absolute newby). I want to open a new tk-window with some text and an image by pressing a Button from another tk-window. I created a code for demonstration.
import tkinter as tk
def fct():
testwin = tk.Tk()
testbutt1 = tk.Button(testwin, text="Button1").pack()
img = tk.PhotoImage(master=testwin,file="picture.png")
imglabel = tk.Label(root,image=img)
imglabel.pack()
testbutt2 = tk.Button(testwin, text="Button2").pack()
testwin.mainloop()
root = tk.Tk()
picbutt = tk.Button(root,text="Exit",command=fct).pack()
label = tk.Label(root)
label.after(5000, fct)
root.mainloop()
This code throws error: "_tkinter.TclError: image "pyimage1" doesn't exist"
When I exchange
img = tk.PhotoImage(master=testwin,file="picture.png")
to
img = tk.PhotoImage(testwin,file="picture.png")
I get error: "images may not be named the same as the main window"
Changing import tkinter as tk
to from tkinter import *
sadly doesn't change anything either. It also doesnt matter if fct()
is called by pressing the button or waiting 5 seconds (label.after(5000, fct)
)
Python stops executing code at this point. Meaning, testbutt1
shows up, testbutt2
does not.
Hope this explaination is enough to describe the probleme. Thanks for your answers :)