0

I'm building a tkinter-window-maker and I'm having trouble with the 'new_window' method within the parent class. I can modify it slightly and copy paste it within each child class to where it works fine but it seems inefficient, if I put the method within the parent class however (code below) it hits the max recursion depth. What's wrong?

Code:

from tkinter import *
import sys

class Window():
    def __init__(self, master, x, y, title):
        self.master = master
        master.title(title)
        master.geometry("{}x{}".format(x,y))

    def new_window(self, class_name):
        self.master.destroy()
        class_name = getattr(sys.modules[__name__], str(class_name))
        root=Tk()
        class_name(root)
        root.mainloop()

class Login(Window):
    def __init__(self, master):
        super().__init__(master, 200, 200, "Login")
        Button(master, text="NEXT",width=7,height=1,command=self.new_window("Home")).grid(row=1,column=1)

class Home(Window):
    def __init__(self, master):
        super().__init__(master, 200, 200, "Home")
        Button(master, text="NEXT",width=7,height=1,command=self.new_window("Login")).grid(row=1,column=1)

def main():
    root=Tk()
    Login(root)
    root.mainloop()

if __name__ == '__main__':
    main()
aLongBoat
  • 59
  • 9
  • You have **Two** `root=Tk()`, thats not possible! – stovfl Dec 08 '18 at 21:02
  • @stovfl So...any hint as to what I should modify? – aLongBoat Dec 08 '18 at 21:49
  • *"what I should modify?"*: **Your whole approach!**. Read [tkinter-understanding-mainloop](https://stackoverflow.com/questions/29158220/tkinter-understanding-mainloop), [follow this pattern](http://effbot.org/tkinterbook/tkinter-hello-again.htm) and [The Tkinter Toplevel Widget](http://effbot.org/tkinterbook/toplevel.htm) – stovfl Dec 09 '18 at 08:34

0 Answers0