Python 3.10.6 /w pyautogui Display Message Boxes, crashes

Question:

Python: 3.10.6,
os: MacOS 12.6,
Software: VS code

I want to confirm before activating the mouse_click()with pyautogui’s message box (.alert or .confirm).

Running the script the message box opens, and if I do nothing its okey. But once I click in the message box, a button or anywhere in the message box window, the ‘spinning wheel’ appears. The application window crashes and needs to ‘Force quit’.

Its the same with every message box.

import pyautogui
import time

def mouse_click():
    while True:
        time.sleep(1)  
        pyautogui.click()

def main():
    pyautogui.confirm('Shall I proceed?')
    mouse_click()

main()

quit()

What might be the issue?

Asked By: NIFIK

||

Answers:

I believe your issue may stem from being in a while loop:

taking it out of the loop seems to fix things. The confirm box also returns the value of the button clicked so I implemented that.

import pyautogui
import time

x = pyautogui.confirm('Shall I proceed?', title='Are you sure?', buttons=['Ok', 'Cancel'])
if x == 'Ok':
    time.sleep(1)
    pyautogui.click()
else:
    quit()

Though for some reason the box doesn’t close until after the click event.
In any event you can work around this by making pyautogui move your mouse and click elsewhere.

Answered By: dan.rad

the solution would be the use of ‘threading’. As having an active GUI and a task running in a loop causes the GUI to freeze, looks to have crashed, until the loop has ended.

some info about threading can be found here Python threading and PySimpleGUI

I’ve moved over to PySimpleGUI and used threading. Explanation and demo code:
https://github.com/PySimpleGUI/PySimpleGUI/blob/master/DemoPrograms/Demo_Multithreaded_Multiple_Threads.py

Answered By: NIFIK
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.