使用tkinter创建新帧后返回初始帧

时间:2014-01-02 19:15:14

标签: python user-interface tkinter

我创建了一个有两个按钮的简单GUI。单击“检查信息”按钮时,将创建一个新帧并覆盖初始帧。这个新框架有一个“返回主页按钮”。我想要做的是破坏Check Info框架并返回初始框架。有人能指出我正确的方向吗?

我按照http://www.tkdocs.com/tutorial/index.html

上的教程了解了这一点

这是我的代码:

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

1 个答案:

答案 0 :(得分:4)

你需要做两件事:

  1. info_frame的每次出现都替换为self.info_frame。这将使框架可通过self访问。

  2. goback内,将self.destroy()替换为self.info_frame.destroy()。这样做会使方法破坏Check Info框架而不是主窗口。

  3. 以下是执行此操作的脚本版本:

    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()
    
相关问题