0

My question seems easy but after trying all methods, which should work, I could not forget or delete child frame.

Program is based on this ex: Switch between two frames in tkinter

My code:

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

The goal is to forget all checkPage frame and move on to PageOne. When I execute the code that I currently have "You are on page one" appears in addition to all widgets from checkPage which I don`t want. Any ideas where I am making mistake?

Nae
  • 14,209
  • 7
  • 52
  • 79
Jonas.S.
  • 155
  • 1
  • 4
  • 14

1 Answers1

2

You didn't specify your parent for the Label, Entry and Button. If you change these 3 lines it should work.

So like this:

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)

ViG
  • 1,848
  • 1
  • 11
  • 13
  • Thanks. It worked. But it seems to me that i overwriting the parent frame: framesForAllWindows. Is this a good practise? or should i create frames separately to all child classes? – Jonas.S. Jan 23 '18 at 18:01
  • @Jonas.S.You're not overwriting the parent frame: `framesForAllWindows`, you're overwriting the passing its parent, `self`, as the parent to lower level children. Which is necessary to have a logical hierarchy. – Nae Jan 23 '18 at 18:05
  • 1
    @Jonas.S. There doesn't seem to be any need for `self.f` at all, since `self` itself is a frame. I recommend getting rid of `self.f` entirely. – Bryan Oakley Jan 23 '18 at 18:14
  • Now it makes sense, because before i could not understand why i needed that frame: self.f That is way i asked about overwriting. Thanks – Jonas.S. Jan 23 '18 at 18:20