0

Okay so I have main window (root) which is essential my main menu. It has buttons that I want to be able to be clicked and start up another window. I know I could achieve this by putting all of the code in one file but I've been told it's messy and bad practice?

So instead in my second document I put the root window in a function and call it in my main document after importing it but get an error:

Traceback (most recent call last):
  File "C:/Users/User/OneDrive/PycharmProjects/PythonProject/main.py", line 22, in <module>
    searchbtn = Button(f1, image=searchImg, text="Search", bg="white")
  File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py", line 2363, in __init__
    Widget.__init__(self, master, 'button', cnf, kw)
  File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py", line 2293, in __init__
    (widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: image "pyimage1" doesn't exist

here is my code:

from tkinter import *
import addRemoveWindow

main = Tk()
main.title("Administrator")
main.configure(background="white")



headerLbl = Label(main, text="Main Menu", font=("arial", 50, "bold"), 
fg="powder blue", bg="white")
headerLbl.grid(sticky="NESW", row=0)

f1 = Frame(main, bg="red")
f1.configure(height=35, width=500)
f1.grid(row=1, columnspan=5)

searchImg = PhotoImage(file="searchuser.png")
searchbtn = Button(f1, image=searchImg, text="Search", bg="white")
searchbtn.grid(row=0, sticky=W, pady=5, padx=5)

addRemImg = PhotoImage(file="ddremove.png")
addRembtn = Button(f1, image=addRemImg ,text="Add/Remove", bg="white", 
command=addRemoveWindow.start_add_remove())
addRembtn.grid(row=0, pady=5, padx=5, sticky=E, column=1)

f2 = Frame(main, bg="blue")
f2.configure(height=20)
f2.grid(row=2, columnspan=5)

searchLabel = Label(f2, text="Search")
searchLabel.grid(row=0, sticky=N)
addRemLabel = Label(f2, text="Search")
addRemLabel.grid(row=0, sticky=N, column=1)


main.mainloop()

I have no idea what is going wrong. I wrote the command in the addRem button but it gives me this error please help!

j_4321
  • 15,431
  • 3
  • 34
  • 61
Cameron Hamilton
  • 29
  • 1
  • 2
  • 8
  • First, when creating `addRembtn`, it should be `command=addRemoveWindow.start_add_remove` without the brackets, otherwise you are calling the function at the button creation. Secondly, do you have two `Tk` instances one in the main file and one in the `addRemoveWindow` module? – j_4321 Nov 28 '17 at 15:11
  • ah yes I hadn't noticed they were in there and yes in the addRemoveWindow the instance is assigned to 'root0' – Cameron Hamilton Nov 28 '17 at 15:18
  • I know there is another way of using tkinter with __init__ but I couldn't understand it and it was working without it. Am I going to have to code that way in order for it to work because I wouldn't even know where to start. – Cameron Hamilton Nov 28 '17 at 15:19
  • 2
    if second program use `Tk()` and `mainloop()` then you may have problem - Tkinter should use only one `Tk()` and one `mainloop()`. – furas Nov 28 '17 at 15:20
  • You should replace one of your `Tk` instances by a `Toplevel` and don't call `mainloop` for this one. – j_4321 Nov 28 '17 at 15:22
  • I saw this error in previous question - error show you have problem with image - maybe you have common problem with PhotoImage which is removed from memory by garbage collector - see Note on page http://effbot.org/tkinterbook/photoimage.htm – furas Nov 28 '17 at 15:22
  • 1
    I think the problem with the image is that it has the wrong master. None was specified at the image creation and since there are two Tk instances, it's ambiguous. So I think that what raised the error is something like you want to display the image in the first `Tk` instance but its master is the second one. – j_4321 Nov 28 '17 at 15:27
  • Okay I will specify the right Tk and see if it helps – Cameron Hamilton Nov 29 '17 at 20:38

0 Answers0