Python/Tkinter - Using multiple classes, causes infinite loop when inheriting Python/Tkinter - Using multiple classes, causes infinite loop when inheriting tkinter tkinter

Python/Tkinter - Using multiple classes, causes infinite loop when inheriting


Your method class shouldn't inherit from the main_window class. Just create an instance of main_window and pass it into the __init__ of method.

BTW, method is a confusing name for a class. Also, class names in Python are conventionally written in CamelCase.


Here's a modified version of your code that illustrates what I mean.

import tkinter as tkimport randomclass MainWindow(object):    def __init__(self):        self.root = tk.Tk()        self.cv = tk.Canvas(self.root, height = 400, width = 400)        self.cv.pack()        draw_stuff = DrawStuff(self)        self.button1 = tk.Button(text="draw line", command=draw_stuff.draw_line)        self.Solve_button_window = self.cv.create_window(5, 5, anchor=tk.NW,  window=self.button1)        self.root.mainloop()class DrawStuff(object):    def __init__(self, mainwin):        self.mainwin = mainwin        self.cv = mainwin.cv    def draw_line(self):        self.point1 = (random.randrange(10, 400), random.randrange(10, 400))        self.point2 = (random.randrange(10, 400), random.randrange(10, 400))        self.cv.create_line(self.point1, self.point2)MainWindow()