python-3.xtkinterdestroyatexit

Python 3 tkinter destroy all child windows at exit of root


SO i have been working on this for a while now and i just have no idea what i am doing wrong. I have a main window my 'root' but i also have a 'helpmenu'. right now, when you close the root helpmenu will remain opened if you opened it. i want helpmenu to close when the root closes.

i have tried to cutout all the lines of code from my program which i believed had no baring on the situation but forgive me if it is still long.

    import atexit
    from tkinter import *

    root = Tk()

    def quit():
        helpmenu.destroy()

    atexit.register(quit)

    def helpmenu():
        helpmenu = Tk()
        helpmenu.geometry('800x600')
        helpmenu.title('Help')
        help_Label = Label(helpmenu, text='Welcome to the Boring Program!')
        help_Label.pack()



    class GUI(object):
        def __init__(self, master):
            self.master = master

            def activateCheck1():
                if c1Var.get() == 1:          #whenever checked
                    c3.config(state=NORMAL)
                elif c1Var.get() == 0:        #whenever unchecked
                    c3.config(state=DISABLED)

            def activateCheck2():
                if c2Var.get() == 1:          #whenever checked
                    c4.config(state=NORMAL)
                elif c2Var.get() == 0:        #whenever unchecked
                    c4.config(state=DISABLED)

            c1Var = IntVar()
            c2Var = IntVar()
            c1_Label = Label(root, text="Select Which Edges to Program")
            c1_Label.grid(row=2, columnspan=2)
            c1 = Checkbutton(root, text="Left Edge", variable=c1Var, command=activateCheck1)
            if parser.get('CONFIGURATION', 'LeftEdgeProgram') == '1':
                c1.select()
            c1.grid(row=3, column=0)
            c2 = Checkbutton(root, text="Right Edge", variable=c2Var, command=activateCheck2)
            if parser.get('CONFIGURATION', 'RightEdgeProgram') == '1':
                c2.select()
            c2.grid(row=3, column=1)

            c3Var = IntVar()
            c4Var = IntVar()
            c3_Label = Label(root, text="Select Which Edges to Insert Glue and Dowels")
            c3_Label.grid(row=4, columnspan=2)
            c3 = Checkbutton(root, text="Left Edge", variable=c3Var)
            if parser.get('CONFIGURATION', 'LeftEdgeDowel') == '1':
                c3.select()
            c3.grid(row=5, column=0)
            c4 = Checkbutton(root, text="Right Edge", variable=c4Var)
            if parser.get('CONFIGURATION', 'RightEdgeDowel') == '1':
                c4.select()
            c4.grid(row=5, column=1)

            menubar = Menu(root)
            filemenu = Menu(menubar, tearoff=0)
            filemenu.add_command(label="Print", state='disabled')
            filemenu.add_command(label="Save Configuration", command=save_config)
            filemenu.add_command(label="Help", command=helpmenu)
            filemenu.add_command(label="Restore Defaults", state='disabled')
            filemenu.add_command(label="About", command=restore_config)
            menubar.add_cascade(label="Options", menu=filemenu)

            root.config(menu=menubar)

    myGUI = GUI(root)
    root.mainloop()

Solution

  • Firstly you are creating two instances of a tk() windows. Creating two tk() windows almost never should be done, this is because two tk windows cannot communicate with eachother. Instead you should use a TopLevel window.

    You should also change your HelpMenu to a class object which inherits the TopLevel Class

    This should look something like this:

    class HelpMenu(Toplevel):
        def __init__(self, master, *args, **kwargs):
            super(HelpMenu, self).__init__(master, *args, **kwargs)
            self.geometry('800x600')
            self.title('Help')
            help_Label = Label(self, text='Welcome to the Boring Program!')
            help_Label.pack()
    

    The only thing that now needs to be changed is the command for the help option in the toolbar

    Something like this (notice I am using a lambda function to make this change):

    filemenu.add_command(label="Help", command= lambda:HelpMenu(self.master) )
    

    This will fix your problem, because when the master window of a TopLevel window is closed the TopLevel window is automatically closed.