Search code examples
python-3.xtkinterinheritanceargumentscustomtkinter

Children class editing parents self in CustomTkinter Python


I know that this problem maybe can be simple, but I'm not a profissional on python. Its is the problem:

I stated a program in Python 3.8.6 with Custom Tkinter, and I'm having a problem with inherit. The Code:

from customtkinter import CTk, CTkButton, CTkFrame


class Base(CTkFrame):
    def __init__(self, master, **kwargs):
        super().__init__(master, **kwargs)

        self.grid_rowconfigure(0, weight=1)
        self.grid_rowconfigure(1, weight=1)
        self.grid_rowconfigure(2, weight=1)
        self.grid_columnconfigure(0, weight=1)

        self.btn_newUser = CTkButton(master=self, command=master.RemoveFrame(master.base),
                                     text="Criação de Usuário", anchor="EW") \
            .grid(row=0, column=0, padx="30", pady="10", sticky="ew")


class App(CTk):
    def __init__(self):
        super().__init__()
        self.title("Crypton - Python Encryptor")
        self.geometry("840x480")
        self._set_appearance_mode("light")

        self.grid_rowconfigure(0, weight=1)
        self.grid_columnconfigure(0, weight=1)

        self.base = Base(master=self)
        self.AddFrame(self.base)

    def RemoveFrame(self, frame):
        frame.grid_forget()

    def AddFrame(self, frame):
        frame.grid(row=0, column=0, sticky="nsew")


if __name__ == "__main__":
    window = App()
    window.mainloop()

I need to forget and add a frame to a grid that is on the parent (App) inside the son (Base), without the parent (App), this is possible?

Obs: I tried to use the grid_forget on the son, but the Frame don't hide.

If have another solution, I'm open, I only need to show multiples screens on a same window in an alternate way.

I tried to create a def inside the parent to the son run, but says that don't have a "base" on "self.base".

Def:

def RemoveBaseFrame(self):
    self.base.grid_forget()

Pycharm Erros:

Cannot find reference 'RemoveFrame' in 'Misc | None' Cannot find reference 'base' in 'Misc | None'


Solution

  • Note that command=master.RemoveFrame(master.base) will executed the function immediately and master.base is not created yet (it is being created).

    Use lambda instead: command=lambda:master.RemoveFrame(master.base).