如何忘记儿童框架?

时间:2018-01-23 17:40:38

标签: python python-3.x tkinter

我的问题似乎很简单,但在尝试了所有可行的方法之后,我无法忘记或删除子框架。

程序基于此例:Switch between two frames in tkinter

我的代码:

import tkinter as tk
from tkinter import ttk


class myProgram(tk.Tk):

    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)

        tk.Tk.title(self, "myProgram")


        framesForAllWindows = tk.Frame(self)
        framesForAllWindows.pack(side="top")
        framesForAllWindows.grid_rowconfigure(0)
        framesForAllWindows.grid_columnconfigure(0)

        self.frames = dict()

        for pages in (checkPage, PageOne):

            frame = pages(framesForAllWindows, self)
            self.frames[pages] = frame
            frame.grid(row=0, column=0, sticky="nsew")

        print(framesForAllWindows.winfo_children())

        self.show_frame(checkPage)


    def show_frame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()


class checkPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        self.f = tk.Frame()
        self.f.pack(side="top")

        self.controller = controller

        self.enterAppid = ttk.Label(text='Please enter your something: ', font=('Courier', 20))
        self.enterAppid.pack(padx=50, pady=100)

        self.appidVar = tk.StringVar()
        self.appidEnter = ttk.Entry(width=60, textvariable=self.appidVar)
        self.appidEnter.pack()

        self.checkAppid = ttk.Button(text='check', command = self.checkInfo)
        self.checkAppid.pack(pady=50, ipady=2)



    def checkInfo(self):

        self.appid = self.appidVar.get()

        if(self.appid == "good"):

            self.controller.show_frame(PageOne)
            #self.f.pack_forget() doesn`t work

        else:
            print('Unknown Error')


class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        label = tk.Label(self, text="You are on page One")
        label.pack(pady=1,padx=10)


app = myProgram()
app.state('zoomed')
app.mainloop()

目标是忘记所有checkPage框架,然后转到PageOne。我执行的代码除了"You are on page one"中我不想要的所有小部件外,还会显示checkPage。我犯错误的任何想法?

1 个答案:

答案 0 :(得分:2)

您没有为LabelEntryButton指定您的父母。如果你改变这3行,它应该可以工作。

所以这样:

self.enterAppid = ttk.Label(self.f, text='Please enter your something: ', font=('Courier', 20))

self.appidEnter = ttk.Entry(self.f, width=60, textvariable=self.appidVar)

self.checkAppid = ttk.Button(self.f, text='check', command = self.checkInfo)