Python Forum

Full Version: maximum recursion depth exceeded
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
whenever i hit the exit button it gives me this error

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\saba_\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 1702, in __call__
return self.func(*args)
File "C:\Users\saba_\Desktop\python\clickerHero.py", line 39, in exit
exit()
File "C:\Users\saba_\Desktop\python\clickerHero.py", line 39, in exit
exit()
File "C:\Users\saba_\Desktop\python\clickerHero.py", line 39, in exit
exit()
[Previous line repeated 985 more times]
RecursionError: maximum recursion depth exceeded

code below

import tkinter as tk
import time
from pynput.mouse import Button, Controller

#creates the window
root = tk.Tk()
root.geometry("600x400")
root.title("attempt1")



#global var

mouse = Controller()
startClickingVar = 0



#def
def startClick():
if startClickingVar == 0:
startClickingVar = 1
while startClickingVar == 1:
time.sleep(1)
mouse.click(Button.left, 1)
time.sleep(1)
else:
pass

def stopClick():
if startClickingVar == 1:
startClickingVar = 0
else:
pass

def exit():
exit()

#


#lables
welcomingLable = tk.Label(root, text="Hi, Welocme to Hero Clicker AutoClicker", font=("arial", 20, "bold"), fg = "blue")
welcomingLable.pack(side=tk.TOP)

#buttons
start = tk.Button(root, text="start", width=25, bg="lightgreen", command=startClick)
start.place(x=50, y=140)

stop = tk.Button(root,text="stop", width = 25, bg="red", command=stopClick)
stop.place(x=350, y=140)

exitButton = tk.Button(root, text="Exit", width = 25, bg="red", command=exit)
exitButton.place(x=200, y=180)


#enterys
numberOfClicks = 1
enterCardInfo = tk.Entry(root, textvariable= numberOfClicks, width=50, bg="orange",)
enterCardInfo.place(x=150, y=100)


# has to stay below so everthing runs within
root.mainloop()
You have this error because the exit() function calls itself recursively. Replace it with
def exit():
    root.destroy()
I changed it and it works, Thank you. but now i'm having the problem of when i hit star the loop starts running and i cant shut it down because the exit button doesn't respond. how can i multi thread a button ?
A good principle is to avoid time.sleep() with GUIs because it freezes the GUI. You can use tkinter's after() method. Something like
startClickingVar = False

def click_forever():
    if startClickingVar:
        mouse.click(Button.left, 1)
        root.after(1000, click_forever)

def startClick():
    global startClickingVar
    if not startClickingVar:
        startClickingVar = True
        click_forever()

def stopClick():
    global startClickingVar
    startClickingVar = False