我正在尝试为光标移动创建一个应用程序,如果用户输入任何数字,请说5并选择一个形状(圆形或正方形):然后鼠标光标必须旋转5次以形成该选定形状。
我收到错误消息:
cursor.setPos((pos [0] + 1,pos [1] + 1))
TypeError:'QPoint'对象不支持索引。
这是我的代码:
import sys
from PyQt4 import QtGui, QtCore
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
lblText = QtGui.QLabel("Enter Number: ", self)
numText = QtGui.QLineEdit(self)
btncir = QtGui.QPushButton('Circle', self)
btncir.setToolTip('Press this button to rotate mouse in circle')
btnsqr = QtGui.QPushButton('Square', self)
btnsqr.setToolTip('Press this button to rotate mouse in square')
fbox = QtGui.QFormLayout()
fbox.addRow(lblText, numText)
fbox.addRow(btncir, btnsqr)
self.setLayout(fbox)
cursor = QtGui.QCursor()
pos = cursor.pos()
cursor.setPos((pos[0] + 1, pos[1] + 1))
self.setWindowTitle('Move Cursor')
self.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
拥有时收到的pos = cursor.pos()
是一个QPoint
实例。要接收的位置QPoint
,您需要使用进行提取
x,y = pos.x(), pos.y()
cursor.setPos(pos.x() + 1, pos.y() + 1)
关于旋转光标。据我了解,您希望光标移动一圈。这是一个小例子,如何实现
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.lblText = QtGui.QLabel("Enter Number: ", self)
self.numText = QtGui.QLineEdit(self)
self.btncir = QtGui.QPushButton('Circle', self)
self.btncir.setToolTip('Press this button to rotate mouse in circle')
self.btncir.connect(self.btncir, QtCore.SIGNAL('clicked()'), self.circleClicked)
self.btnsqr = QtGui.QPushButton('Square', self)
self.btnsqr.setToolTip('Press this button to rotate mouse in square')
fbox = QtGui.QFormLayout()
fbox.addRow(self.lblText, self.numText)
fbox.addRow(self.btncir, self.btnsqr)
self.setLayout(fbox)
self.cursor = QtGui.QCursor()
self.setWindowTitle('Move Cursor')
self.show()
def circleClicked(self):
# Grab number of rotations
n=int(str(self.numText.text()))
# Define circle
angle=np.linspace(-np.pi,np.pi,50)
radius=10.
# Get Cursor
pos = self.cursor.pos()
X=pos.x()
Y=pos.y()
# Loop through repitions
for i in range(n):
# Loop through angles
for phi in angle:
# New coordinate
x=X+radius*np.cos(phi)
y=Y+radius*np.sin(phi)
# Update position
self.cursor.setPos(x,y)
# Sleep a bit so we can see the movement
time.sleep(0.01)
请注意,我将的所有窗口小部件属性Example
设为,这使得使用的方法更易于访问它们Example
。另请注意,QCursor.setPos
它不使用tuple
,而是两个整数作为输入。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句