Python 当通过PyQt4按钮运行时,脚本不工作
我正在尝试在单击的x和y位置记录鼠标单击。脚本本身工作正常,但当链接到PyQt4按钮时,它会冻结GUI中的显示。并且不会产生错误。如何让此脚本通过GUI运行 在此处输入代码:Python 当通过PyQt4按钮运行时,脚本不工作,python,python-3.x,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):
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(0x01)、win32api.GetKeyState(0x02))
类点(结构):
_字段=[(“x”,c_长),(“y”,c_长)]
def查询使用位置(自身):
pt=点()
windell.user32.GetCursorPos(byref(pt))
返回{“x”:pt.x,“y”:pt.y}
def mousecapturesequence(自身、状态左、状态右):
x=1
而(x<3):
a=win32api.GetKeyState(0x01)
如果是状态#左:#按钮状态已更改
状态_左=a
印刷品(a)
如果a<0:
打印('按下左键')
pos=self.queryMousePosition()
打印(pos)
x=x+1
其他:
打印('左按钮已释放')
睡眠时间(0.001)
def main():
app=QtGui.QApplication(sys.argv)
ex=示例()
sys.exit(app.exec_())
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
main()
不要在回调中创建事件轮询循环。当然,这会冻结GUI。不要在回调中创建事件轮询循环。当然,这会冻结GUI。
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()