0

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 :)

  • 3
    Why are you creating two instances of `Tk`? You should never do that; it can cause problems (like this one). If you want to make a second window, use `Toplevel` instead. Check out [this question](https://stackoverflow.com/q/23224574/16775594), and the comments beneath it. – Sylvester Kruin Mar 14 '22 at 14:50
  • That helped a lot! Thank you. I created two instances of Tk, because I didn't know a better way to open new windows. ;) – Bokkwurst Mar 14 '22 at 16:09
  • No problem! It's an easy mistake to make. By the way, welcome to SO! I suggest you read the [tour] for a basic intro to how things are run here. You should also check out the [help], specifically [ask], for information on asking questions. You are also strongly encouraged to [research](https://meta.stackoverflow.com/q/261592/16775594) before asking, so as to avoid asking duplicate questions. But good job explaining your problem, showing your attempts to solve it, and properly formatting your code! That stuff can be hard to nail! – Sylvester Kruin Mar 14 '22 at 16:15

1 Answers1

-2

I want to open a new tk-window with some text and an image by pressing a Button from another tk-window.

@Sylvester Kruin say, "Why are you creating two instances of Tk"?

The problem can be fixed.

Simply, by putting all widgets outside of the function. And use that .configure or .config inside function to prevent duplicating widgets.

Snippet:

import tkinter as tk

def fct():
    imglabel.configure(image=img)
    
    testbutt1.configure(text="Button1")
     
    testbutt2.configure(text="Button2")
    
    testbutt1.pack()
    testbutt2.pack()
       
root = tk.Tk()
img = tk.PhotoImage(file="p1.png")

picbutt = tk.Button(root,text="Exit",command=fct).pack()

testbutt1 = tk.Button(root)
 
testbutt2 = tk.Button(root)
 
imglabel = tk.Label(root )
imglabel.pack()

label = tk.Label(root)
label.after(5000, fct)

root.mainloop()

Screenshot:

enter image description here

toyota Supra
  • 3,181
  • 4
  • 15
  • 19