Search code examples
pythontkinterwindowchildrengrandchild

Python: Closing a sub-child-window prevents the opening of a new sub-child


For my job in a laboratory of my University of Applied Sciences I need to create a Python-programm which creates a child-windows with the possibility to create another one.

So far this works quite fine.

The tricky thing is where I close the childrens child and try to open a new "grandchild" of the main-window.

Closing and opening also works fine on the level of the first child. I can enter that child, go back to the main menu and so on as long I wish.

Here the code I am working on right now:

import tkinter


def Praktika():
   global Praktika
   Praktika = tkinter.Toplevel(main)
   Praktika.geometry("320x200")

   Prak1 = tkinter.Button(Praktika, text="Praktikum 1", command =Praktikum1)
   Prak1.pack()
   Haupt = tkinter.Button(Praktika, text="Hauptmenu", command = ClosePraktika)
   Haupt.pack()


def ClosePraktika():
    Praktika.destroy()

def Praktikum1():
   global Praktikum1
   Praktikum1 = tkinter.Toplevel(main)
   Praktikum1.geometry("320x200")

   Haupt = tkinter.Button(Praktikum1, text="Hauptmenu", command = ClosePraktikum1)
   Haupt.pack()

def ClosePraktikum1():
   Praktika.destroy()
   Praktikum1.destroy()

def CloseAll():
   main.quit()

main = tkinter.Tk()
main.geometry("320x200")
main.title("Fueh")
tkinter.Button(main, text="Praktika", command=Praktika).pack()
tkinter.Button(main, text="Exit", command=CloseAll).pack()
main.mainloop()

This is now the third attempt until now and ffter the research I have done I start to think that handling sub-children ain't that easy as I think.

So well, already thank you very much for the help!


Solution

  • The problem is that you have a function named Praktikum1, and then you create a global variable named Praktikum1 which causes the function to be destroyed. So, the next time you call the function, you're actually "calling" the variable.

    Don't use the same name for global variables and for functions.