2016-09-13 2 views
0

私はPython 2.7とPyQT4を使用しています。私は単純な電卓を作っています。私は2つのQLineEditを持っていて、追加の結果を出力する関数を持っています。QLineEditの値を別の関数に渡すにはどうすればいいですか?

class Window(QtGui.QMainWindow): 
    global number_1_text 
    global number_2_text 

    def __init__(self): 
     super(Window, self).__init__() 
     self.setWindowFlags(QtCore.Qt.WindowMinimizeButtonHint) 
     #Main Window Settings 
     self.setGeometry(50,50,500,300) 
     self.setWindowTitle("Point of sale !") 
     self.setWindowIcon(QtGui.QIcon('python.png')) 

     #Close Action 
     exitAction = QtGui.QAction(QtGui.QIcon('python.png'),"Close Now !", self) 
     exitAction.setShortcut("Ctrl+Q") 
     exitAction.setStatusTip('Leave the Application') 
     exitAction.triggered.connect(self.close_application) 
     self.statusBar() 

     #Menubar 
     mainMenu = self.menuBar() 
     fileMenu = mainMenu.addMenu('&File') 
     fileMenu.addAction(exitAction) 


     # Create textboxs 
     number_1 = QtGui.QLineEdit(self) 
     number_1.move(20, 50) 
     number_1.resize(380,40) 
     self.number_1_text = number_1.text() 

     number_2 = QtGui.QLineEdit(self) 
     number_2.move(20, 100) 
     number_2.resize(380,40) 
     self.number_2_text = number_2.text() 

     #Main Home Proccess 
     quiteBtn = QtGui.QPushButton("Quite", self) 
     quiteBtn.resize(100, 50) 
     quiteBtn.move(50,220) 
     quiteBtn.clicked.connect(self.close_application) 

     addBtn = QtGui.QPushButton("Add", self) 
     addBtn.resize(100, 50) 
     addBtn.move(150,220) 
     addBtn.clicked.connect(self.addNumbers) 
     self.show() 

    def addNumbers(self): 
     print self.number_1_text 
     print self.number_2_text 
     print "Done" 

    #Close The Whole Application 
    def close_application(self): 
     choice = QtGui.QMessageBox.question(self, 'Exit !', 'Are you sure you wanna exit?', 
      QtGui.QMessageBox.Yes | QtGui.QMessageBox.No 
      ) 
     if choice == QtGui.QMessageBox.Yes: 
      sys.exit() 
     else: 
      pass 

私はその後、私はaddNumbers関数内で値を呼び出そうとしましたが、私は空の値を取得し、text()機能を使用して値を取得しようとしましたが、クラスのグローバル変数に割り当てられています。

結果のスクリーンショット: Result

答えて

0

あなたはあなたの子供は、メインウィンドウの属性をウィジェットにする必要があります。そうすれば、あなたは後で簡単にアクセスすることができます

class Window(QtGui.QMainWindow): 
    def __init__(self): 
     super(Window, self).__init__() 
     ... 
     self.number_1 = QtGui.QLineEdit(self) 
     self.number_1.move(20, 50) 
     self.number_1.resize(380, 40) 

     self.number_2 = QtGui.QLineEdit(self) 
     self.number_2.move(20, 100) 
     self.number_2.resize(380, 40) 
     ... 

    def addNumbers(self): 
     a = float(self.number_1.text()) 
     b = float(self.number_2.text()) 
     print '%s + %s = %s' % (a, b, a + b) 
     print 'Done' 
+0

は、私はあなたのソリューション を試してみました。しかし、私は トレースバック(最新の呼び出しの最後)だ: ファイル "C:\ Users \ユーザーLilEssamデスクトップの\ pos.py \" を、 0行目9、 クラスウィンドウ(QtGui.QMainWindow): ファイル "C:\ Users \ LilEssam \ Desktop \ pos.py"、行54、ウィンドウ b = float(self.number_2.text()) NameError:名前 'self'は定義されていません –

+0

@AhmedEssam。これは私の例でコードを使用しなかったからです。 'addNumbers'は' Window'クラスのメソッドでなければなりません。 – ekhumoro

+0

ありがとうございました!それは今働いています、ありがとう! –

関連する問題