2017-02-16 56 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 我輸入了正確的庫嗎? –