Camilla Tac
Camilla Tac

Reputation: 21

Image not Showing on Canvas when opening New Tkinter Window

I am creating a GUI using TKinter, the main window can open new instances of itself from its main menu.

This works however when I create a new instance of a window the image that should appear on the canvas doesn't appear. Is there anyone that can help?

This is my GUI code:

class MainPage: 

    def __init__(self, master):
        master.title("EIL Viewer")
        master.geometry('1000x650')

        #creating the main menu
        master.option_add('*tearOff', False)
        mainmenu = Menu(master)
        master.configure(menu = mainmenu)
        File = Menu(mainmenu)
        mainmenu.add_cascade(menu= File, label = 'File')

        #setting up commands for main menu
        File.add_command( label = 'New Window', command = lambda: New() , accelerator = 'Ctrl + N')

        #creating the canvas on the window
        self.canvas = Canvas(master)
        self.canvas.pack()
        self.canvas.pack_configure(fill= BOTH, expand = True)
        self.canvas.config(width=300, height= 500,background = 'white')


        #setting the image on the canvas
        photo = PhotoImage( file = 'C:/Users/Admin/Documents/Untitled Folder/sampleimage.png')
        self.canvas.img = photo #here I am storing the image to the canvas so that it stays in the memory 
        self.image = self.canvas.create_image(150,150, image = self.canvas.img)

This is how I code the function which opens a new instance from the main menu:

    #setting n = 0 counter to distinguish the different windows
n = 0 

def New():
    n = Tk()

    mainpage = MainPage(n)

    n.mainloop()

    n = n + 1 

The reason for which I set the counter n = n + 1 is so that each time a new instance of my window is called it will not run the window passing the same master window. Is this superfluous?

When I try to run the New window command from the menu I get the following error regarding the image loaded onto the canvas:

Exception in Tkinter callback
Traceback (most recent call last):
  File "<ipython-input-3-803bc8733dc3>", line 60, in <lambda>
    File.add_command( label = 'New Window', command = lambda: New() , accelerator = 'Ctrl + N')
  File "<ipython-input-3-803bc8733dc3>", line 1425, in New
    mainpage = MainPage(n)
  File "<ipython-input-3-803bc8733dc3>", line 480, in __init__
    self.eil = self.canvas.create_image(150,150, image = self.canvas.img)
  File "C:\Users\Camilla Tac\Anaconda3\lib\tkinter\__init__.py", line 2483, in create_image
    return self._create('image', args, kw)
  File "C:\Users\Camilla Tac\Anaconda3\lib\tkinter\__init__.py", line 2474, in _create
    *(args + self._options(cnf, kw))))
_tkinter.TclError: image "pyimage16" doesn't exist

Can anyone help me solve this issue???

Upvotes: 0

Views: 695

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385870

You cannot share images between two instances of Tk. A proper Tkinter GUI should always have exactly once instance of Tk. If you need multiple windows, every window except the first needs to be an instance of Toplevel.

Upvotes: 1

Related Questions