How can I stop the thread when the user clicks the stop button of my auto clicker? How would I restart the thread after the thread was stopped then started again.
I tried separating it into two classes one for gui and one for auto clicker functionality. when I tried to run a thread in the gui using a target function from the auto clicker class. It doesn’t click but when I merged the function into the same class as GUI, it clicks. I ran into the problem of stopping the auto clicker in the middle of the repeats. If the repeats for example is 5, auto clicker won’t stop until it clicked 5 times. I tried adding a break in the while and for loop. It breaks after one more click but I tried time.sleep() for 3 seconds it may delay the click interval
def main(self):
self.window_settings()
self.window.mainloop()
def auto_type(self):
type_frame = tk.LabelFrame(self.window, text="Auto Type", bg="white", height=45, width=514, bd=5)
type_frame.place(x=5, y=275)
clicker_type = tk.Radiobutton(self.window, text="Auto Clicker", bg="white", variable=self.autokind, font=("Arial", 12), value=0)
clicker_type.place(x=120, y=290)
key_type = tk.Radiobutton(self.window, text="Key Presser", bg="white", variable=self.autokind, font=("Arial", 12), value=1)
key_type.place(x=300, y=290)
#Setting the default to being auto clicker
self.autokind.set("0")
#Start button for either the auto clicker or key presser
start_button = tk.Button(self.window, text="Start", width=24, height=3, bd=0, command=self.turn_on)
start_button.place(x=5, y=330)
#Stop button for either the auto clicker or key presser
stop_button = tk.Button(self.window, text="Stop", width=24, height=3, bd=0, command=self.turn_off)
stop_button.place(x=265, y=330)
#Turning auto on
def turn_on(self):
self.on = True
if self.worker1.is_alive() == False:
self.worker1.start()
#Turning the auto off
def turn_off(self):
self.on = False
def start_clicking(self):
#Setting a counter if the user is planning to repeat a certain times
counter = 0
seconds = self.total_seconds()
repeats = int(self.total_click_repeat())
x_coor = int(self.xcoordinate())
y_coor = int(self.ycoordinate())
#If its follow mouse, this runs
if self.mousewhere() == "0":
#Making sure the repeats aren't blank
if self.clickrepeat_type() == "0" and repeats != 0:
while counter < repeats:
if self.on == False:
break
if self.click_type() == "Single":
if self.mouse_button() == "Middle":
#Pausing by the interval
time.sleep(seconds)
pyautogui.middleClick()
counter += 1
else:
time.sleep(seconds)
pyautogui.click(button=(self.mouse_button().lower()))
counter += 1
elif self.click_type() == "Double":
time.sleep(seconds)
pyautogui.doubleClick(button=(self.mouse_button().lower()))
counter += 1
#Resetting the counter to 0
counter = 0
#Stopping the auto clicker once the number of repeats is reached
self.on = False
elif self.clickrepeat_type() == "1":
while self.on == True:
if self.click_type() == "Single":
if self.mouse_button() == "Middle":
#Pausing by the interval
time.sleep(seconds)
pyautogui.middleClick()
else:
time.sleep(seconds)
pyautogui.click(button=(self.mouse_button().lower()))
elif self.click_type() == "Double":
time.sleep(seconds)
pyautogui.doubleClick(button=(self.mouse_button().lower()))
#If its coordinate, this runs
elif self.mousewhere() == "1":
if self.clickrepeat_type() == "0" and repeats != 0:
while counter < repeats:
if self.on == False:
break
if self.click_type() == "Single":
if self.mouse_button() == "Middle":
#Pausing by the interval
time.sleep(seconds)
pyautogui.middleClick(x=x_coor, y=y_coor)
counter += 1
else:
time.sleep(seconds)
pyautogui.click(button=(self.mouse_button().lower()), x=x_coor, y=y_coor)
counter += 1
elif self.click_type() == "Double":
time.sleep(seconds)
pyautogui.doubleClick(button=(self.mouse_button().lower()), x=x_coor, y=y_coor)
time.sleep()
in the Tkinter handler?