脚本在由PyQt4按钮运行时不起作用

时间:2017-07-30 16:35:22

标签: python python-3.x

我试图在点击的x和y位置记录鼠标点击。该脚本可以自行运行,但是当链接到PyQt4按钮时,它会冻结GUI。并且不会产生错误。如何通过GUI运行此脚本?

在此处输入代码:

import sys
from PyQt4 import QtGui, QtCore
import win32api
import time
from ctypes import windll, Structure, c_long, byref

class Example(QtGui.QMainWindow):

    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):      

        btn1 = QtGui.QPushButton("Button 1", self)
        btn1.move(30, 50)

        btn1.clicked.connect(self.run_script)            

        self.statusBar()

        self.setGeometry(300, 300, 290, 150)
        self.setWindowTitle('Event sender')
        self.show()

    def run_script(self):

mousecapturesequence(win32api.GetKeyState(0×01),win32api.GetKeyState(0×02))

    class POINT(Structure):
        _fields_ = [("x", c_long), ("y", c_long)]

    def queryMousePosition(self):
        pt = POINT()
        windll.user32.GetCursorPos(byref(pt))
        return { "x": pt.x, "y": pt.y}

    def mousecapturesequence(self,state_left,state_right):
        x = 1
        while (x < 3):
            a = win32api.GetKeyState(0x01)
            if a != state_left:  # Button state changed
                state_left = a
                print(a)
                if a < 0:
                    print('Left Button Pressed')
                    pos = self.queryMousePosition()
                    print(pos)
                    x = x + 1
                else:
                    print('Left Button Released')

            time.sleep(0.001)

def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

0 个答案:

没有答案