首页 > 解决方案 > 在类中使用 tk 时,tkinter destroy() 不起作用

问题描述

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

我试图找出一种制作退出按钮的方法,但我实施的所有方法都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()

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

标签: pythontkinter

解决方案


您是否使用按钮按下命令或访问 root_window 进行了尝试?

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

当按下按钮时调用()完成


推荐阅读