python - 在类中使用 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()
如果有人知道如何用这个制作一个销毁按钮,那将是一个巨大的帮助!(:
解决方案
您是否使用按钮按下命令或访问 root_window 进行了尝试?
button['command'] = root_window.destroy # 给它函数
当按下按钮时调用()完成
推荐阅读
- angular - angular 5 个组件在一个组件中发生变化,然后反映在其他兄弟姐妹上
- javascript - 图表消失,按下提交按钮时
- java - 为什么我在屏幕上拖动的物体抖动得这么厉害?
- javascript - javascript倒数计时器删除小时分钟秒..过期后
- ubuntu - 用于 linux 的 windows 子系统的磁盘空间(如果我安装了 2 个 WSL 发行版)
- python - Pyspark - 将另一列添加到稀疏向量列
- reactjs - 等待自定义登录登录
- django - 在 cookie 中保存身份验证令牌(Django Rest Framework + React)
- regex - 考虑 Powershell 正则表达式中的行返回
- ios - iframe 在 ios 中无法滚动