Home > Software engineering >  I want to hide my main window and then open new window(tkinter)
I want to hide my main window and then open new window(tkinter)

Time:07-26

Could you help me? I'm trying to close my main window and then create a new window. I'm using withdraw() instead of destroy() since I'm planning to use that widget later.

Here is my code, but I just get: tkinter.TclError: image "pyimage10" doesn't exist

I separated the codes of the main window and a new window into two python file, which are "Page1" and "Page2".

Sorry for my rough English. Any help to fix this would be much appreciated:)

tkinter.TclError: image "pyimage10" doesn't existseems to occur at image_2 = canvas.create_image(

Page1

    from tkinter import Tk, Canvas, Entry, Text, Button, PhotoImage

   OUTPUT_PATH = Path(__file__).parent
   ASSETS_PATH = OUTPUT_PATH / Path("./assets")



   def relative_to_assets(path: str) -> Path:
       return ASSETS_PATH / Path(path)





   window = Tk()

   window.geometry("703x981")
   window.configure(bg = "#FFFFFF")

     button_image_6 = PhotoImage(
         file=relative_to_assets("button_6.png"))
     button_6 = Button(
         image=button_image_6,
         borderwidth=0,
         highlightthickness=0,
         command=lambda: ("WM_DELETE_WINDOW", nextPage()),
         relief="flat"
     )
     button_6.place(
         x=415.0,
         y=793.0,
         width=86.0,
         height=78.0
     )

    
    def nextPage():
        window.withdraw()
        import Page2
    
    
   

Page2

 

from tkinter import Tk, Canvas, Entry, Text, Button, PhotoImage
    
window = Tk()

def relative_to_assets(path: str) -> Path:
    return ASSETS_PATH / Path(path)



window.geometry("545x470")
window.configure(bg = "#FFFFFF")

window.title('PythonGuides')
canvas = Canvas(
    window,
    bg = "#FFFFFF",
    height = 470,
    width = 545,
    bd = 0,
    highlightthickness = 0,
    relief = "ridge"
)

canvas.place(x = 0, y = 0)
image_image_2 = PhotoImage(
    file=relative_to_assets("image_2.png"))
image_2 = canvas.create_image(
    272.0,
    175.0,
    image=image_image_2
)

CodePudding user response:

Maybe it's happening because the image you are trying to add on the button is not present where you are trying to access it from.

And seems you have missed this:

  1. You didn't pack the button

  2. You didn't run the .mainloop() i.e window.mainloop()

  3. and what does the :

    [enter the image description here][1]

is meant to do ?

I didn't use this and the code ran perfectly fine.

CodePudding user response:

This error is caused by having multiple instances of Tk. Having multiple instances of Tk can cause errors. Use Toplevel instead for additional windows.

Change window = Tk() to window = Toplevel() in Page2. You will also need to add Toplevel to your import list.

  • Related