2017-02-16 57 views
0

我想为光标移动创建一个应用程序,如果用户输入任何数字,比如说5 &选择一个形状(圆形或方形):然后鼠标光标必须旋转5次使选定的形状。如何在PyQt4中以圆形旋转鼠标

我得到错误:

cursor.setPos((pos[0] + 1, pos[1] + 1))
TypeError: 'QPoint' object does not support indexing.

这是我的代码:

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

回答

1

,当你有pos = cursor.pos()你收到什么是QPoint实例。要接收QPoint的位置,您需要使用

x,y = pos.x(), pos.y() 

又见herehere提取它。所以在你的代码示例中,你可能想要做

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,而是两个整数作为输入。

+0

感谢您的时间,但再次添加此它给我的错误.. 错误: cursor.setPos((pos.x + 100,+ pos.y 100)) 类型错误:不支持的操作数类型(S)为+:“builtin_function_or_method”和“INT” 我的主要目的是无论如何旋转圆周运动的光标。你能帮助我吗?感谢您的回复:) –

+0

您收到错误是因为我犯了一个错误。 ''pos.x''返回方法,'pos.x()''的值。我做了一个编辑,为你写了一个小例子。 – alexblae

+0

我试图运行由u ..所做的代码,它没有错误执行,但我没有得到任何输出。 我导入这些库以运行该代码: import numpy as np import time 我输入了正确的库吗? –