Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何忘记一个孩子的框架?_Python_Python 3.x_Tkinter - Fatal编程技术网

Python 如何忘记一个孩子的框架?

Python 如何忘记一个孩子的框架?,python,python-3.x,tkinter,Python,Python 3.x,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, "myProgr

我的问题似乎很简单,但在尝试了所有应该有效的方法之后,我不能忘记或删除子帧

该计划基于以下示例:

我的代码:

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

目标是忘记所有的
检查页面
框架,然后转到
PageOne
。当我执行当前拥有的代码时,
“您在第一页”
会出现在我不想要的
checkPage
中的所有小部件之外。知道我哪里出错了吗

您没有为
标签
条目
按钮
指定父项。如果您更改这3行,它应该可以工作

所以像这样:

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

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


self.checkAppid=ttk.Button(self.f,text='check',command=self.checkInfo)
您没有为
标签、
条目和
按钮指定父项。如果您更改这3行,它应该可以工作

所以像这样:

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

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


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

谢谢。成功了。但在我看来,我覆盖了父框架:framesForAllWindows。这是个好习惯吗?或者我应该为所有子类分别创建帧吗?@Jonas.S.您没有覆盖父帧:
framesForAllWindows
,您正在覆盖将其父级
self
作为父级传递给较低级别的子级。这是具有逻辑层次结构所必需的。@Jonas.S。似乎根本不需要
self.f
,因为
self
本身就是一个框架。我建议完全去掉self.f
。谢谢,谢谢。成功了。但在我看来,我覆盖了父框架:framesForAllWindows。这是个好习惯吗?或者我应该为所有子类分别创建帧吗?@Jonas.S.您没有覆盖父帧:
framesForAllWindows
,您正在覆盖将其父级
self
作为父级传递给较低级别的子级。这是具有逻辑层次结构所必需的。@Jonas.S。似乎根本不需要
self.f
,因为
self
本身就是一个框架。我建议完全去掉self.f
。谢谢