当同时发生屏幕捕获和按键操作时,PyQt5 GUI冻结

时间:2019-05-12 16:27:22

标签: python pyqt

我正在尝试按特定的时间间隔捕获整个桌面的屏幕截图,并连续读取按键。

在QLineEdit中键入文本时,如果同时发生按键和屏幕捕获,则GUI会完全冻结。

我尝试了枕头/ mss截屏,并尝试了PyUserInput / PyHook捕获按键。桂冻结所有组合。完整的代码示例如下:

from PyQt5 import QtCore, QtGui, QtWidgets
from pykeyboard import PyKeyboardEvent
from PyQt5.QtCore import QThread, pyqtSignal, pyqtSlot
from mss import mss
import sys

class MonitorSuper(PyKeyboardEvent):
    def tap(self, keycode, character, press):
        print(keycode)

class Main_thread(QThread):
    signal = pyqtSignal('PyQt_PyObject')
    def __init__(self):
        QThread.__init__(self)
        None

    def run(self):
        while(1):
            mon = MonitorSuper()
            mon.run()

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.resize(535, 207)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
        self.box_work = QtWidgets.QLineEdit(self.centralwidget)
        self.box_work.setStyleSheet("border: 2px solid grey")
        self.gridLayout.addWidget(self.box_work, 1, 0, 1, 2)
        MainWindow.setCentralWidget(self.centralwidget)
        self.thread_key = Main_thread()
        self.thread_key.start()
        self.timer = QtCore.QTimer()
        self.timer.timeout.connect(self.take_snap)
        self.timer.start(2000)

    def take_snap(self):       
        filename = r'C:\Users\www\Desktop\NewImage.png'
        print(filename)
        filename = mss().shot(mon=-1, output=filename)

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

0 个答案:

没有答案