I'm a python noob and apologize if the solution to my question is obvious. None of the other solutions I've tried seemed to work and I've been stumped on this issue for almost the entire day now. Anyway, I'm trying to create a simple auto clicker that clicks in random intervals with a given range. Functionally, I have everything working but I cannot get my program to stop clicking once it starts.
Here is my clicking function:
def start_clicking(event=None):
min = min_var.get()
max = max_var.get()
def report_click():
text1.insert(tk.INSERT, "\nTime since last click: " + range_str_s + " (" + range_str_ms + ")\n")
text1.config(state="normal")
try: # Show exception to user
min = float(min)
max = float(max)
except ValueError:
text1.insert(tk.INSERT, "One or both of your inputs are invalid.")
if running: # For some reason, this runs even though running is False. I only want this to run while running is true
random_range = random.uniform(min, max)
random_range_s_to_ms = int(random_range * 1000) # Convert seconds to ms
range_str_s = "{:,.2f}s".format(random_range)
range_str_ms = "{:,.2f}ms".format(random_range_s_to_ms)
print(running)
report_click()
pyautogui.click()
root.after(random_range_s_to_ms, start_clicking) # Recursively call start_clicking
text1.see("end")
text1.config(state="disabled")
This is my stop function:
def stop_clicking(event=None):
text1.config(state="normal")
text1.insert(tk.INSERT, "\nStopping...\n")
global running
running = False # This is not doing what I want it to do.
text1.see("end")
text1.config(state="disabled")
Some things I noted while trying to get this to work is that all of the code under my if running condition in my start_clicking() function still runs despite the fact that it's false (or so I think). I confirmed this by printing it into the console. Whether or not running is true or false seems to have no effect on my code. I commented this into in my program to clarify where the issue is.
Here is a minimal reproducible example:
import pyautogui, time, random, keyboard
import tkinter as tk
root = tk.Tk()
root.geometry("530x320")
min_var = tk.StringVar()
max_var = tk.StringVar()
min_var_entry = tk.Entry(root, textvariable=min_var, font=('calibre', 10, 'normal'), width=8)
max_var_entry = tk.Entry(root, textvariable=max_var, font=('calibre', 10, 'normal'), width=8)
min_var_entry.insert(tk.INSERT, "0")
max_var_entry.insert(tk.INSERT, "1")
def start_clicking_task(event=None):
global running
running = True
start_clicking()
def stop_clicking(event=None):
text1.insert(tk.INSERT, "\nStopping...\n")
global running
running = False
print("Running is currently ", running)
text1.see("end")
text1.config(state="disabled")
def force_close(even=None):
start_btn.master.destroy() # This destroys the entire window
def start_clicking_event(event=None):
global running
running = True
def start_clicking(event=None):
global running
running = True
min = min_var.get()
max = max_var.get()
min = float(min)
max = float(max)
if running is True:
random_range = random.uniform(min, max)
random_range_s_to_ms = int(random_range * 1000) # Convert seconds to ms
range_str_s = "{:,.2f}s".format(random_range)
range_str_ms = "{:,.2f}ms".format(random_range_s_to_ms)
pyautogui.click()
root.after(random_range_s_to_ms, start_clicking) # This continues to run
text1.insert(tk.INSERT, "\nTime since last click: " + range_str_s + " (" + range_str_ms + ")\n") # But this stops printing
print("Running is currently ", running)
text1 = tk.Text(root, height=20, width=41, bg="#A4B0BD")
keyboard.add_hotkey('f2', start_clicking)
keyboard.add_hotkey('f3', stop_clicking)
keyboard.add_hotkey('esc', force_close)
text1.config(state="normal")
text1.grid(row=1, column=0, rowspan=10,padx=10)
range_label = tk.Label(root, text="Click Interval\n(seconds)", font=('calibre', 10, 'bold'), bg="#0A3D62", width=11, fg="#7ddeff")
min_label = tk.Label(root, text="Min", font=('calibre', 10, 'bold'), bg="#0A3D62", width=3, fg="#7ddeff")
max_label = tk.Label(root, text="Max", font=('calibre', 10, 'bold'), bg="#0A3D62", width=3, fg="#7ddeff")
start_btn = tk.Button(root, text='Start(F2)', bg="#2C3335", width=7, fg="#7ddeff", command=start_clicking_event)
stop_btn = tk.Button(root, text='Stop(F3)', bg="#2C3335", width=7, fg="#7ddeff", command=stop_clicking)
force_close_btn = tk.Button(root, text='Force close\n(Esc)', bg="#2C3335", width=8, fg="#7ddeff", command=force_close)
range_label.grid(row=1, column=2)
min_label.grid(row=2, column=1)
min_var_entry.grid(row=2, column=2)
max_label.grid(row=3, column=1)
max_var_entry.grid(row=3, column=2)
start_btn.grid(row=2, column=3, padx=3)
stop_btn.grid(row=3, column=3, padx=3)
force_close_btn.grid(row=8, column=2)
root.mainloop()