tkinter destroy() not working when using tk in a class

Question:

This is not my original code but you can see me trying to make sense and to expand on the code with the comments.

I was trying to figure out a way to make a quit button but all the ways I was implementing destroy() didn’t work.

#tkinter imports
from tkinter import *
from tkinter import messagebox
from tkinter.messagebox import showinfo
from tkinter import simpledialog
from tkinter import colorchooser
from tkinter import ttk

 
#pygame / andra imports
import pygame
import os

#tkinter variabler
bgColor = "#FFFFFF"                        #Background färg
fgColor = "#2b2b2b"                        #Font färg
text = "Arial 12"                

class Application(Tk):
    def __init__(self):
        Tk.__init__(self)
        self._frame = None
        self.switch_frame(StartMenu)
        

    def switch_frame(self, frame_class):
        new_frame = frame_class(self)
        if self._frame is not None:
            self._frame.destroy()
        self._frame = new_frame
        self._frame.pack()


class StartMenu(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
        Frame.winfo_toplevel(self).title("Generic Gladiator Game | GGG")    #Titeln för gui:n
        self.master.configure(bg = bgColor)                                 #Background färg på appen
        Frame.configure(self,bg = bgColor)                                  #Background färg på items backgrunden
        Frame.winfo_toplevel(self).geometry("800x600")                      #Storleken på fönstret
        #Frame.configure(self,height = 100)


        Label(self,
                text="Generic Gladiator Game",
                fg = fgColor,
                bg = bgColor,
                font = "Helvetica 32 bold italic", padx=50,pady=50).pack()

        Button(self,  #(New Game) går till setup för ett nytt spel
                fg = fgColor,
                bg = bgColor,
                width = 15,
                height = 1,
                relief = "solid",
                borderwidth = "1",
                highlightthickness=0,
                activebackground="grey94",
                font = text,
                text="New Game",
                command=lambda: master.switch_frame(NewGame)).pack(side="top", pady=15)

        Button(self,  #(Contiune) fortsätter ett nytt spel
                fg = fgColor,
                bg = bgColor,
                width = 15,
                height = 1,
                relief = "solid",
                borderwidth = "1",
                highlightthickness=0,
                activebackground="grey94",
                font = text,
                text="Continue",
                command=lambda: master.switch_frame(Continue)).pack(side="top", pady=15)
        
        Button(self,  #(Quit) stänger av applikationen 
                fg = fgColor,
                bg = bgColor,
                width = 15,
                height = 1,
                relief = "solid",
                borderwidth = "1",
                highlightthickness=0,
                activebackground="grey94",
                font = text,
                text = "Quit",
                command = lambda: master.switch_frame(Continue)).pack(side="top", pady=15)




class NewGame(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
        self.master.configure(bg = bgColor)                                 #Background färg på appen
        Frame.configure(self,bg = bgColor)                                  #Background färg på items backgrunden
                
        Label(self,
                text="Generic Gladiator Game",
                fg = fgColor,
                bg = bgColor,
                font = "Helvetica 32 bold italic", padx=50,pady=50).pack()

        Button(self,
                fg = fgColor,
                bg = bgColor,
                width = 10,
                height = 1,
                relief = "solid",
                borderwidth = "1",
                highlightthickness=0,
                activebackground="grey94",
                font = text,
                text = "Cancel",
                command = lambda: master.switch_frame(StartMenu)).pack(side="top")

class Continue(Frame):
    def __init__(self, master):
            Frame.__init__(self, master)
            self.master.configure(bg = bgColor)                                 #Background färg på appen
            Frame.configure(self,bg = bgColor)                                  #Background färg på items backgrunden


            Label(self,
                    text="Generic Gladiator Game",
                    fg = fgColor,
                    bg = bgColor,
                    font = "Helvetica 32 bold italic", padx=50,pady=50).pack()

            Button(self, 
                    fg = fgColor,
                    bg = bgColor,
                    width = 10,
                    height = 1,
                    relief = "solid",
                    borderwidth = "1",
                    highlightthickness=0,
                    activebackground="grey94",
                    font = text,
                    text = "Cancel",
                    command = lambda: master.switch_frame(StartMenu)).pack(side="top")

#Funktioner

if __name__ == "__main__":
    app = Application() #master klassen för start menyn 
    app.mainloop()

If anyone knows how to make a destroy button with this it would be a huge help! (:

Asked By: nomad

||

Answers:

Did you try it with a button press command or accessing the root_window?

button[‘command’] = root_window.destroy # give it the function

when the button is pressed the call () is done

Answered By: Richard Taujenis

You can add a button in the Application class

class Application(Tk):
    def __init__(self):
        Tk.__init__(self)
        self._frame = None
        self.switch_frame(StartMenu)
        Button(self, text='Quit', command=self.destroy).pack()
Answered By: Reblochon Masque
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.