2015-08-28 38 views
2

嗨只是有一個問題捕捉實例時,用戶輸入一個字母,而不是數字。獲取錯誤'不能分配數字到函數調用'pythonQt捕捉非整數輸入

這是一個簡單的兩個QLine編輯框,用戶必須猜測數字......任何幫助,將不勝感激。剛剛發佈的功能位

def my_response(): 
     global counter, random_number 
     timer=QtCore.QTimer 

     try: 
      reply.text()= int(reply.text()) 

      if int(reply.text()) <self. random_number: 
       question.setText("Your guess is too low. Please Try Again") 
       reply.setText(" ") 


      elif int(reply.text()) >self. random_number: 
       question.setText("Your guess is too high. Please Try Again") 
       reply.setText(" ") 


      elif int(reply.text()) ==self. random_number: 
       question.setStyleSheet('QLineEdit{color: blue}') 
       question.setFont(QtGui.QFont("Stencil", 15, QtGui.QFont.Bold)) 
       question.setText("Correct") 
       timer.singleShot(250,lambda:question.setText('You won')) 
       reply.setText(" ") 

     except: 
      reply.setText('invalad input number is needed') 

回答

0

錯誤是在

reply.text()= int(reply.text()) 

的消息很簡單 - 你不能分配數量函數調用。 (實際上,你不能分配任何東西給函數調用。)

還要注意,你可以將inputMaskvalidator分配給你的QLineEdit,你根本不需要檢查無效輸入。