PySide2: window is not responding

Viewed 194

Problem

Window is not responding when opened before the mouse.wait function. If replace the mouse.wait function to another (which doesn't waiting the mouse click), the window will open normally.

import pyautogui, sys, mouse
from PySide2 import QtWidgets
from design import Ui_Form, Ui_Next

def mpos(file):
    mouse.wait(button='right', target_types='down')
    x,y=pyautogui.position()
    file.write(str(x)+'_'+str(y)+'-')

def prog():
    with open('prog.txt', 'w') as file:
        next()
        mpos(file)
        next()
        mpos(file)
        next()
        mpos(file)

def menu():
    mui.configButton.clicked.connect(prog)
    wmenu.show()

def fclose():
    wnext.close()

def next():
    nui.okButton.clicked.connect(fclose)
    wnext.show()

app = QtWidgets.QApplication(sys.argv)

wmenu = QtWidgets.QFrame()
mui = Ui_Form()
mui.setupUi(wmenu)

wnext = QtWidgets.QFrame()
nui = Ui_Next()
nui.setupUi(wnext)

menu()
sys.exit(app.exec_())
1 Answers

The problem is that the wait method blocks the Qt event loop by freezing the GUI. A possible solution is to run it in another thread:

import threading

# ...

def execute_thread():
    threading.Thread(target=prog, daemon=True).start()

def menu():
    mui.configButton.clicked.connect(execute_thread)

# ...
Related