Linked Questions

Popular Questions

I'm writing a small application on Python.

I have a While loop executing indefinitely while a global variable is set to True, but I want to be able to set this same variable as False clicking another button (I'm using dearpygui to make a interface).

Here's the main function code:

import pyautogui
import time

global enabled
enabled = True

def stopautoclick():
    listglobals = globals()
    listglobals["enabled"] = False

def autoclick(button, delay, coordX, coordY):
    listglobals = globals()
    mousebutton = button
    match mousebutton:
        case "Esquerdo":
            mousebutton = "left";
        case "Direito":
            mousebutton =  "right";
        case "Centro":
            mousebutton = "middle";
    print(mousebutton)
    time.sleep(delay)
    while listglobals["enabled"]:       
            pyautogui.click(x=coordX, y=coordY, button=mousebutton);
            time.sleep(delay);

See that enabled global variable? So, I created a button to stop that while iteration by setting this global variable to False, like this:

dpg.add_button(label="Parar Auto Click", callback=lambda: stopautoclick)

Problem is nothing in my code executes while the while loop iterates. Therefore, when I click the stop button, it doesn't execute anything and the value of the global variable enabled is not changed.

Even tried using the asyncio library to make the code execute asynchronously, but no success.

TL;DR: I want to be able to stop a loop (be a while or a for loop, doesn't matter) by clicking a button in my window interface, but while the loop is running, nothing else executes.

Related Questions