Python 使用tkinter创建新帧后返回初始帧
我创建了一个简单的GUI,它有两个按钮。单击“检查信息”按钮时,将创建一个新帧并覆盖在初始帧上。这个新框架有一个“返回主页按钮”。我要做的是销毁Check Info帧并返回初始帧。有人能给我指出正确的方向吗 我是通过在 这是我的密码:Python 使用tkinter创建新帧后返回初始帧,python,user-interface,tkinter,Python,User Interface,Tkinter,我创建了一个简单的GUI,它有两个按钮。单击“检查信息”按钮时,将创建一个新帧并覆盖在初始帧上。这个新框架有一个“返回主页按钮”。我要做的是销毁Check Info帧并返回初始帧。有人能给我指出正确的方向吗 我是通过在 这是我的密码: from tkinter import * from tkinter import ttk class Gui(Tk): def __init__(self, parent): Tk.__init__(self, parent)
from tkinter import *
from tkinter import ttk
class Gui(Tk):
def __init__(self, parent):
Tk.__init__(self, parent)
self.parent = parent
self.initialize()
def initialize(self):
initframe = ttk.Frame(self, padding="3 3 12 12")
initframe.grid(column=0, row=0, sticky=(N, W, E, S))
initframe.columnconfigure(0, weight=1)
initframe.rowconfigure(0, weight=1)
self.resizable(False, False)
ttk.Button(initframe, text="Check Info", command=self.check_info).grid(column=2, row=2, sticky=(W, E))
ttk.Button(initframe, text="Run Tests" ).grid(column=4, row=2, sticky=(W, E))
for child in initframe.winfo_children():
child.grid_configure(padx=20, pady=20)
self.update()
self.geometry(self.geometry())
self.bind('<Return>', self.check_info_enter)
def goback(self):
self.destroy()
def check_info(self):
info_frame = ttk.Frame(self, padding="3 3 12 12")
info_frame.grid(column=0, row=0, sticky=(N, W, E, S))
info_frame.columnconfigure(0, weight=1)
info_frame.rowconfigure(0, weight=1)
self.resizable(False, False)
ttk.Button(info_frame, text="Return Home", command=self.goback).grid(column=2, row=2, sticky=(W, E))
def check_info_enter(self, event):
self.update()
if __name__ == "__main__":
app = Gui(None)
app.title('Initial')
app.mainloop()
从tkinter导入*
从tkinter导入ttk
类Gui(Tk):
定义初始化(自身,父级):
Tk.\uuuuuuuuuuuuuuuuuuuuu初始(自我,父)
self.parent=parent
self.initialize()
def初始化(自):
initframe=ttk.Frame(self,padding=“3 3 12”)
网格(列=0,行=0,粘性=(N,W,E,S))
initframe.columnconfigure(0,权重=1)
initframe.rowconfigure(0,权重=1)
自我调整大小(False,False)
按钮(initframe,text=“Check Info”,command=self.Check\u Info).grid(列=2,行=2,粘性=(W,E))
ttk.Button(initframe,text=“runtests”).grid(列=4,行=2,粘性=(W,E))
对于initframe.winfo_children()中的子项:
配置子网格(padx=20,pady=20)
self.update()
self.geometry(self.geometry())
self.bind(“”,self.check\u info\u enter)
def goback(自我):
自我毁灭
def检查信息(自身):
info_frame=ttk.frame(self,padding=“3 3 12”)
信息框架网格(列=0,行=0,粘性=(N,W,E,S))
info_frame.columnconfigure(0,权重=1)
info_frame.rowconfigure(0,权重=1)
自我调整大小(False,False)
ttk.Button(info_frame,text=“Return Home”,command=self.goback).grid(column=2,row=2,sticky=(W,E))
def检查信息输入(自身、事件):
self.update()
如果名称=“\uuuuu main\uuuuuuuu”:
app=Gui(无)
附录标题(“首字母”)
app.mainloop()
您需要做两件事:
info\u frame
替换为self.info\u frame
。这将使框架可通过self
访问goback
内部,将self.destroy()
替换为self.info\u frame.destroy()
。这样做将使该方法销毁检查信息
框架,而不是主窗口from tkinter import *
from tkinter import ttk
class Gui(Tk):
def __init__(self, parent):
Tk.__init__(self, parent)
self.parent = parent
self.initialize()
def initialize(self):
initframe = ttk.Frame(self, padding="3 3 12 12")
initframe.grid(column=0, row=0, sticky=(N, W, E, S))
initframe.columnconfigure(0, weight=1)
initframe.rowconfigure(0, weight=1)
self.resizable(False, False)
ttk.Button(initframe, text="Check Info", command=self.check_info).grid(column=2, row=2, sticky=(W, E))
ttk.Button(initframe, text="Run Tests" ).grid(column=4, row=2, sticky=(W, E))
for child in initframe.winfo_children():
child.grid_configure(padx=20, pady=20)
self.update()
self.geometry(self.geometry())
self.bind('<Return>', self.check_info_enter)
def goback(self):
self.info_frame.destroy()
def check_info(self):
self.info_frame = ttk.Frame(self, padding="3 3 12 12")
self.info_frame.grid(column=0, row=0, sticky=(N, W, E, S))
self.info_frame.columnconfigure(0, weight=1)
self.info_frame.rowconfigure(0, weight=1)
self.resizable(False, False)
ttk.Button(self.info_frame, text="Return Home", command=self.goback).grid(column=2, row=2, sticky=(W, E))
def check_info_enter(self, event):
self.update()
if __name__ == "__main__":
app = Gui(None)
app.title('Initial')
app.mainloop()
从tkinter导入*
从tkinter导入ttk
类Gui(Tk):
定义初始化(自身,父级):
Tk.\uuuuuuuuuuuuuuuuuuuuu初始(自我,父)
self.parent=parent
self.initialize()
def初始化(自):
initframe=ttk.Frame(self,padding=“3 3 12”)
网格(列=0,行=0,粘性=(N,W,E,S))
initframe.columnconfigure(0,权重=1)
initframe.rowconfigure(0,权重=1)
自我调整大小(False,False)
按钮(initframe,text=“Check Info”,command=self.Check\u Info).grid(列=2,行=2,粘性=(W,E))
ttk.Button(initframe,text=“runtests”).grid(列=4,行=2,粘性=(W,E))
对于initframe.winfo_children()中的子项:
配置子网格(padx=20,pady=20)
self.update()
self.geometry(self.geometry())
self.bind(“”,self.check\u info\u enter)
def goback(自我):
self.info_frame.destroy()
def检查信息(自身):
self.info_frame=ttk.frame(self,padding=“3 3 12”)
self.info_frame.grid(列=0,行=0,粘性=(N,W,E,S))
self.info\u frame.columnconfigure(0,权重=1)
self.info\u frame.rowconfigure(0,权重=1)
自我调整大小(False,False)
ttk.按钮(self.info_frame,text=“Return Home”,command=self.goback).grid(列=2,行=2,粘性=(W,E))
def check\u info\u enter(自、事件):
self.update()
如果名称=“\uuuuu main\uuuuuuuu”:
app=Gui(无)
附录标题(“首字母”)
app.mainloop()
您需要做两件事:
info\u frame
替换为self.info\u frame
。这将使框架可通过self
访问goback
内部,将self.destroy()
替换为self.info\u frame.destroy()
。这样做将使该方法销毁检查信息
框架,而不是主窗口from tkinter import *
from tkinter import ttk
class Gui(Tk):
def __init__(self, parent):
Tk.__init__(self, parent)
self.parent = parent
self.initialize()
def initialize(self):
initframe = ttk.Frame(self, padding="3 3 12 12")
initframe.grid(column=0, row=0, sticky=(N, W, E, S))
initframe.columnconfigure(0, weight=1)
initframe.rowconfigure(0, weight=1)
self.resizable(False, False)
ttk.Button(initframe, text="Check Info", command=self.check_info).grid(column=2, row=2, sticky=(W, E))
ttk.Button(initframe, text="Run Tests" ).grid(column=4, row=2, sticky=(W, E))
for child in initframe.winfo_children():
child.grid_configure(padx=20, pady=20)
self.update()
self.geometry(self.geometry())
self.bind('<Return>', self.check_info_enter)
def goback(self):
self.info_frame.destroy()
def check_info(self):
self.info_frame = ttk.Frame(self, padding="3 3 12 12")
self.info_frame.grid(column=0, row=0, sticky=(N, W, E, S))
self.info_frame.columnconfigure(0, weight=1)
self.info_frame.rowconfigure(0, weight=1)
self.resizable(False, False)
ttk.Button(self.info_frame, text="Return Home", command=self.goback).grid(column=2, row=2, sticky=(W, E))
def check_info_enter(self, event):
self.update()
if __name__ == "__main__":
app = Gui(None)
app.title('Initial')
app.mainloop()
从tkinter导入*
从tkinter导入ttk
类Gui(Tk):
定义初始化(自身,父级):
Tk.\uuuuuuuuuuuuuuuuuuuuu初始(自我,父)
self.parent=parent
self.initialize()
def初始化(自):
initframe=ttk.Frame(self,padding=“3 3 12”)
网格(列=0,行=0,粘性=(N,W,E,S))
initframe.columnconfigure(0,权重=1)
initframe.rowconfigure(0,权重=1)
自我调整大小(False,False)
按钮(initframe,text=“Check Info”,command=self.Check\u Info).grid(列=2,行=2,粘性=(W,E))
ttk.Button(initframe,text=“runtests”).grid(列=4,行=2,粘性=(W,E))
对于initframe.winfo_children()中的子项:
配置子网格(padx=20,pady=20)
self.update()
self.geometry(self.geometry())
self.bind(“”,self.check\u info\u enter)
def goback(自我):
self.info_frame.destroy()
def检查信息(自身):
self.info_frame=ttk.frame(self,padding=“3 3 12”)
self.info_frame.grid(列=0,行=0,粘性=(N,W,E,S))
self.info\u frame.columnconfigure(0,权重=1)
self.info\u frame.rowconfigure(0,权重=1)
自我调整大小(False,False)
ttk.按钮(self.info_frame,text=“Return Home”,command=self.goback).grid(列=2,行=2,粘性=(W,E))
def检查信息输入(自身、事件):
self.update()
如果名称=“\uuuuu main\uuuuuuuu”:
app=Gui(无)
附录标题(“首字母”)
app.mainloop()
goback()
将破坏根窗口(在本例中为self
),您可能希望