How to fix RecursionError: maximum recursion depth exceeded in instancecheck
There is a part of the code
class Creat_Timetable():
def __init__(self):
self.all_hours_start = [8,9,10,11,12,13,14]
self.all_minutes_start = [28,23,28,28,23,28,23]
Creat_Timetable.CreatWin(self)
def CreatWin(self): # Создаём главное окно
global main_win, time
main_win = Tk ()
main_win.resizable ( width=False,height=False )
main_win.geometry ( "700x400")
main_win.title ( "Timetable" )
time = Label ( text="" )
time.place ( x=323,y=0)
Monday = Label (main_win, text="понедельник" )
Tuesday = Label (main_win, text="вторник" )
Wednesday = Label (main_win, text="среда" )
Thursday = Label (main_win, text="четверг" )
Friday = Label (main_win, text="пятница" )
Monday.place ( x=10,y=50 )
Tuesday.place ( x=190,y=50 )
Wednesday.place ( x=340,y=50 )
Thursday.place ( x=450,y=50 )
Friday.place ( x=580,y=50 )
Creat_Timetable.CheckTime ( self)
main_win.mainloop ()
def CheckTime(self): # Проверка времяни для сбора информации
now = datetime.now ()
if now.minute < 10:
time_n = str ( now.hour )+":0"+str ( now.minute )
else:
time_n = str(now.hour) + ":" + str(now.minute)
time.configure(text=str(time_n))
for i in range(0,len(self.all_hours_start)):
if self.all_minutes_start[i] < 10:
time_l = str (self.all_hours_start[i] )+":0" + str(self.all_minutes_start[i])
else:
time_l = str(self.all_hours_start[i]) + ":" + str(self.all_minutes_start[i])
if time_n == time_l: # Если время нужное
print ( app2.schoolParse () ) # Выводим что спарсила программа
del self.all_hours_start[i], self.all_minutes_start[i] # Удаляем не нужное время
main_win.after(100000,Creat_Timetable.CheckTime(self))
Returns the error
Traceback (most recent call last): File "/Users/lrd/PycharmProjects/schedule_creation/venv/include/main.py", line 65, in Creat_Timetable() File "/Users/lrd/PycharmProjects/schedule_creation/venv/include/main.py", line 11, in init Creat_Timetable.CreatWin(self) File "/Users/lrd/PycharmProjects/schedule_creation/venv/include/main.py", line 36, in CreatWin Creat_Timetable.CheckTime ( self) File "/Users/lrd/PycharmProjects/schedule_creation/venv/include/main.py", line 62, in CheckTime main_win.after(100000,Creat_Timetable.CheckTime(self)) File "/Users/lrd/PycharmProjects/schedule_creation/venv/include/main.py", line 62, in CheckTime main_win.after(100000,Creat_Timetable.CheckTime(self)) File "/Users/lrd/PycharmProjects/schedule_creation/venv/include/main.py", line 62, in CheckTime main_win.after(100000,Creat_Timetable.CheckTime(self)) [Previous line repeated 988 more times] File "/Users/lrd/PycharmProjects/schedule_creation/venv/include/main.py", line 50, in CheckTime time.configure(text=str(time_n)) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/init.py", line 1485, in configure return self._configure('configure', cnf, kw) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/init.py", line 1469, in _configure cnf = _cnfmerge((cnf, kw)) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/init.py", line 100, in _cnfmerge elif isinstance(cnfs, (type(None), str)): RecursionError: maximum recursion depth exceeded in instancecheck
1 answers
In this line:
main_win.after(100000,Creat_Timetable.CheckTime(self))
You have the CheckTime
method called recursively, then its result is passed to the after
method (more precisely, it is never passed because of infinite recursion). In after
, you need to pass the method itself, without calling it:
main_win.after(100000, self.CheckTime)
In general, the constructions of the form ИмяКласса.метод(self)
should be replaced with self.метод()
everywhere-this is the same in essence, but it is easier to read this way. Calling a method through a class only makes sense for class methods and static methods (i.e., methods with annotations @classmethod
and @staticmethod
).
I would also advise you to get rid of using global variables when using OOP. Use object fields instead of global variables.