First, I apologize if I've omitted necessary code. I'm a beginner to coding and this is my first time developing a GUI app -- I'm not sure if I'm showing what you need to assist me.

I am trying to develop a multi-window GUI application for data entry at my company. I have been using page to build pages. I currently have 3 pages, which work individually, and a variety of functions to import and save data. Now I am trying to build a function that hides all widgets on the root window so that I can create/show a new Toplevel without the old widgets being visible.

ClassB contains all widgets for my root window. I am trying to call ClassA.main_continue, which I want to display pageC widgets and hide all root/classB widgets.

I am receiving an error "maximum recursion depth exceeded while calling a Python object." Python alternates between trying to execute the lines labeled "#####...1" and "#####..2" before exiting with that error.

I have spent 10 hours researching and trying different methods to open a new window and hide the old window. I will eventually have 6-8 windows so I want a separate class that I can use to control them all.

import tkinter as tk

root = tk.Tk()
    top = ClassB (root)

class ClassA: #class that contains functions to control program/do tasks
    def main_continue(self, top):
        ClassC(root)
        x = ClassB(top) ################################## 1
        self.x.main_button1.place_forget()

class ClassB: #root window
    def __init__(self, top=None):
        y = ClassA()

        self.main_button1 = tk.Button(top, command= y.main_continue(top)) ######################2
        self.main_button1.pack()
        self.main_button1.place(relx=0.383, rely=0.5, height=24, width=140)
        self.main_button1.configure(text='''Continue''')

class ClassC: #2nd page
    def __init__(self, top=None):
        ## code for a different page of widgets

1 Answers

0
Mike - SMT On Best Solutions

First I want to address a few of your issues in your code. self.x.main_button1.place_forget() will cause an error as self.x does not exist. You need to define x as a class attribute first by applying the self. prefix. Next you are using pack() and place() on your button. You do not need to do this. You only need to use one of the 3 geometry managers. My personal preference is grid() I find it to be the easiest to use for my layouts. Actually you can reduce your 4 lines of code down to 1 line for that button.

If you are going to use classes to build your GUI I would recommend inherit from Tkinter classes such as the Tk() instance and Frame or Toplevel.

Here is a super simple example of switching frames. There are many ways you can do this but this should be a good start for you.

import tkinter as tk


root = tk.Tk()
main_page = tk.Frame(root)
main_page.grid(row=0, column=0)
second_page = tk.Frame(root)


def show_main_page():
    second_page.grid_forget()
    main_page.grid(row=0, column=0)


def show_second_page():
    main_page.grid_forget()
    second_page.grid(row=0, column=0)

tk.Button(main_page, text='Show 2nd page', command=show_second_page).grid(row=0, column=0)
tk.Button(second_page, text='Show Main page', command=show_main_page).grid(row=0, column=0)

root.mainloop()