简体   繁体   中英

PySide2: window is not responding

问题

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_())

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)

# ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM