Home > other >  Is mainloop() causing a RecursionError?
Is mainloop() causing a RecursionError?

Time:12-22

import tkinter 
from tkinter import ttk    
class GUI(tkinter.Tk):
    def __init__(self):
        self = tkinter.Tk()       
gui = GUI()
gui.mainloop()  

Running the code above causes RecursionError. If I take out gui.mainloop(), there is no RecursionError and a single window is displayed, as intended. It seems, then, that gui.mainloop() is causing too many windows to be made and displayed, resulting in RecursionError. How is this possible, and what can I do to fix this?

CodePudding user response:

This is not the correct way to inherit from a widget. You shouldn't reassign self, and you shouldn't create an instance of Tk inside Tk.

The proper way to write this short program is as follows:

import tkinter
from tkinter import ttk
class GUI(tkinter.Tk):
    def __init__(self):
        super().__init__()
gui = GUI()
gui.mainloop()
  • Related