0

I always get an error message when I try to insert a background picture in my second window. I don't get this error message if I insert it in my first window.

from tkinter import *

tkFenster1 = Tk()
tkFenster1.title('LoveResult')
tkFenster1.geometry('300x300')
tkFenster1.configure(bg='')

schalke = PhotoImage(file='hallo.gif')
my_canvasschalke = Canvas(master=tkFenster1, width=300, height=300)
my_canvasschalke.pack(fill='both', expand=True)
my_canvasschalke.create_image(0,0, image=schalke, anchor='nw')
Exception in Tkinter callback
  Traceback (most recent call last):
  
  File "<ipython-input-1-24d984b0ec0f>", line 62, in buttonCalculateClick
    schalke = PhotoImage(file='hallo.gif')

  File "C:\Users\tjark\anaconda3\lib\tkinter\__init__.py", line 4061, in __init__

Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "C:\Users\tjark\anaconda3\lib\tkinter\__init__.py", line 4006, in __init__

    self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't open "hallo.gif": no such file or directory
Nat Riddle
  • 928
  • 1
  • 10
  • 24
  • Are you sure you are in the right directory when running the file? – TheLizzard May 28 '21 at 15:27
  • I have two Tk's. I can add text to the second Tk, but no pictures. How can i add a second Tk? – Tjark Bunjes May 28 '21 at 15:32
  • tkFenster1 = Tk() tkFenster1.title('Hamburg') tkFenster1.geometry('300x300') tkFenster = Tk() tkFenster.title('LOVE CALCULATOR') tkFenster.geometry('1280x853') tkFenster.configure (bg= '#fefefe') – Tjark Bunjes May 28 '21 at 15:34
  • Only one `Tk()` allowed in your script, use `Toplevel()` for second window. – Jason Yang May 28 '21 at 15:37
  • I Love You Jason Yang – Tjark Bunjes May 28 '21 at 15:57
  • @JasonYang That is incorrect. You can have as many as you need (usually you want to use `Toplevel`s) but you always have to pass in the `master` keyword argument – TheLizzard May 28 '21 at 16:08
  • Just to show the class required, no detail for arguments, IMO, it doesn't mean no argument. I didn't say only one Toplevel allowed. – Jason Yang May 28 '21 at 16:25
  • @JasonYang You said: *"Only one `Tk()` allowed in your script"*. That isn't true. You can have as many as you need. Although `Toplevel` will also create a new window. – TheLizzard May 28 '21 at 16:29
  • Please refer the reply from Bryan Oakley https://stackoverflow.com/questions/17525869/can-you-combine-two-tkinter-tk-widgets/17531244 or https://stackoverflow.com/questions/48045401/why-are-multiple-instances-of-tk-discouraged – Jason Yang May 28 '21 at 16:33
  • When i'm running it with my own gif file it just works fine! A fix could be to change the path to an absolute path (r"C:\Users\Whatever\hallo.gif") – Itsjul1an May 28 '21 at 16:37
  • @JasonYang: that first answer you linked to was subtly wrong. I've updated the answer. It's possible to have multiple instances of `Tk`, but that's not how tkinter was designed to be used. – Bryan Oakley May 28 '21 at 17:57
  • The error message means exactly what it says - the script can't find the given file. Are you aware that it's looking in the _current working directory_ which may not be the same as the folder where the script exists? – Bryan Oakley May 28 '21 at 17:59
  • Just checked what happened if two `Tk()` created and each one with one Canvas, then `create_image` on canvas1 will be OK, but exception `_tkinter.TclError: image "pyimage1" doesn't exist` if `create_image` on canvas2. It is same as the description in this issue, althought different exception message. – Jason Yang May 28 '21 at 18:10
  • In 3rd comment from Tjark Bunjes, we can find 2 `Tk()` created. – Jason Yang May 28 '21 at 18:12
  • @Bryan Oakley: Just remember one post somewhere from you, so not to use more than one `Tk()`. – Jason Yang May 28 '21 at 18:17
  • @JasonYang In your testing script add `master=` to the `PhotoImage(...)`. That will fix the `_tkinter.TclError: image "pyimage1" doesn't exist` error. So as long as you always pass in the `master` keyword arguemnt when creating `PhotoImage`s/tkinter variables you shouldn't have any problems with having multiple instances of `Tk()`. For some reason people don't pass in the `master` argument and blame it on the second instance of `Tk()` – TheLizzard May 28 '21 at 18:20
  • Yes, you are right ! Maybe @Tjark Bunjes can do the samething in his script. – Jason Yang May 28 '21 at 18:26
  • 1
    @JasonYang OP's problem is different. `tkinter` can't access `hallo.gif` for some reason. Maybe OP is running the code from the wrong directory or the file isn't where OP expects it to be... – TheLizzard May 28 '21 at 18:28
  • "I Love You Jason Yang" replied from @Tjark Bunjes, maybe my answer did help, just maybe. – Jason Yang May 28 '21 at 18:31

1 Answers1

-2

Hi you just can use Tk() once if you want make child window you should use Toplevel() if you are working with functions you should make Background global in Child window function section and thats it , background image will be shown on Child window

Amin
  • 34
  • 4