2016-09-13 3 views
0

파이썬 2.7과 PyQT4를 사용하고 있는데, 나는 계산기를 두 개 가지고 있습니다. 두 개가 있습니다. 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 

내가 text() 함수 값을 얻을 클래스의 글로벌 변수에 할당하려고, 그때 addNumbers 기능의 값을 호출하려고하지만 난 빈 값을 얻는다.

결과의 스크린 샷 : 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 : \ 사용자 \ LilEssam 바탕 화면 \의 pos.py \"를, 라인 9 클래스 창의 (QtGui.QMainWindow) 파일 "C : \ 사용자 \ LilEssam 화상 \의 pos.py \", 라인 (54), 창에 B = 플로트 (self.number_2.text()) NameError : name 'self'가 정의되지 않았습니다. –

+0

@AhmedEssam. 내 예제에서 코드를 사용하지 않았기 때문입니다.'addNumbers'는'Window' 클래스의 메소드 여야합니다. – ekhumoro

+0

감사합니다. 지금 일하고있어, 고마워! –

관련 문제