Python tkinter destroy()在类中使用tk时不起作用

Python tkinter destroy()在类中使用tk时不起作用,python,tkinter,Python,Tkinter,这不是我的原始代码,但您可以看到我试图理解并用注释扩展代码 我试图找到一种方法来创建一个退出按钮,但是我实现destroy()的所有方法都不起作用 #tkinter imports from tkinter import * from tkinter import messagebox from tkinter.messagebox import showinfo from tkinter import simpledialog from tkinter import colorchooser

这不是我的原始代码,但您可以看到我试图理解并用注释扩展代码

我试图找到一种方法来创建一个退出按钮,但是我实现
destroy()
的所有方法都不起作用

#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()

如果有人知道如何用它制作一个破坏按钮,那将是一个巨大的帮助!(:

您是否使用按钮按下命令或访问根窗口进行了尝试

按钮['command']=root_window.destroy#给它函数


按下按钮后,调用()完成您可以在
应用程序
类中添加一个按钮

class Application(Tk):
    def __init__(self):
        Tk.__init__(self)
        self._frame = None
        self.switch_frame(StartMenu)
        Button(self, text='Quit', command=self.destroy).pack()

谢谢你的帮助!