7

This is where the error is found:

global backbuttonimg
backbuttonimg = PhotoImage(file="backbutton.gif")
C6 = tkinter.Button(W_CheckDates, image=backbuttonimg, command = CheckDatesBack)
C6.pack()

I don't understand why this isn't working. I have another image in my program here:

def Login():
  global W_Menu
  W_Menu = Tk()
  W_Menu.geometry('160x310+600+200')
  W_Menu.title("NSS DB")
  A0 = Canvas(W_Menu, width='160', height='160')
  A0.pack()
  global img
  img = PhotoImage(file="nsslogo.gif")
  A0.create_image(80,80, image=img)

I also get a similar error when I try to call the above definition after it has already been initially called (for example when my program logs out) so I have readjusted so the window simply deiconifies instead of calling it again, and I do not get the error again. However I am confused as to why I get an error with the former section of code now, as the button simply does not show up whether it is called for the first time or not. Sorry if this is a bit vague, please ask if I have not explained in enough detail. Thanks in advance.

P.S. I have looked in other threads with similar problems but none apply to me.

user3112327
  • 275
  • 3
  • 6
  • 14

5 Answers5

19

Ok so you say that the login function works once, then it can't work again. Here the problem can be solved using tk.Toplevel() instead of tk.Tk() see: why python photoimages don't exist? and tkinter.TclError: image "pyimage3" doesn't exist

These threads mention how you can't have two instances of Tk() running simultaneously, you have to use Toplevel() instead.

Why did these threads not apply to you (i think they do...)? But just a tip, if you state that they don't apply to you, then give reasons why, it helps make your question clearer. Also, add the full traceback when your question is about a particular error.

Hope this helps a bit.

Community
  • 1
  • 1
W1ll1amvl
  • 1,219
  • 2
  • 16
  • 30
  • 1
    Thanks a lot, works really well! I am very new to coding so just an error on my part - they did apply to me. Hopefully next time this won't happen :) – user3112327 Oct 02 '14 at 14:35
2

You can add a master parameter

backbuttonimg = PhotoImage(file="backbutton.gif",master=W_Menu)
1

Adding this for anyone who has tried the above with no success. If you have an erroneous path when running the script in some environments the path to the file is retained. I commented out everything from where I first use PhotoImage up to the window mainloop, run the script, close resulting gui, uncomment the code, run, and it shows the image as expected.

Windy71
  • 851
  • 1
  • 9
  • 30
0

Here Cledia I am also facing this error. And when I use Toplevel instead of To, it is working well I suggest you to use TopleToplevel

Goochi
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – KiynL May 17 '22 at 06:02
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/31785360) – Emi OB May 19 '22 at 07:13
0

Use tk.Toplevel() instead of tk.Tk() So its will work because the python coding library tkinter it doesnt make any sens if you make two windows working sametime with the tk.Tk() so you can use the toplevel() to open multiwindows in the same time !! Hope it's Helpful