Assigning value to the variable in Qt
Question: Here is the converted .ui of Qt to the .py:

    
    
    from PyQt4 import QtCore, QtGui
    
    try:
      _fromUtf8 = QtCore.QString.fromUtf8
    except AttributeError:
      _fromUtf8 = lambda s: s
    
    class Ui_NewWindow(object):
      def setupUi(self, NewWindow):
        NewWindow.setObjectName(_fromUtf8("NewWindow"))
        NewWindow.resize(439, 225)
        self.centralwidget = QtGui.QWidget(NewWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.pushButton_2 = QtGui.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(170, 140, 99, 27))
        self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
        self.widget = QtGui.QWidget(self.centralwidget)
        self.widget.setGeometry(QtCore.QRect(40, 30, 365, 89))
        self.widget.setObjectName(_fromUtf8("widget"))
        self.verticalLayout = QtGui.QVBoxLayout(self.widget)
        self.verticalLayout.setMargin(0)
        self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
        self.horizontalLayout_2 = QtGui.QHBoxLayout()
        self.horizontalLayout_2.setObjectName(_fromUtf8("horizontalLayout_2"))
        self.label_3 = QtGui.QLabel(self.widget)
        self.label_3.setObjectName(_fromUtf8("label_3"))
        self.horizontalLayout_2.addWidget(self.label_3)
        self.lineEdit = QtGui.QLineEdit(self.widget)
        self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
        self.horizontalLayout_2.addWidget(self.lineEdit)
        self.label_4 = QtGui.QLabel(self.widget)
        self.label_4.setObjectName(_fromUtf8("label_4"))
        self.horizontalLayout_2.addWidget(self.label_4)
        self.lineEdit_2 = QtGui.QLineEdit(self.widget)
        self.lineEdit_2.setObjectName(_fromUtf8("lineEdit_2"))
        self.horizontalLayout_2.addWidget(self.lineEdit_2)
        self.verticalLayout.addLayout(self.horizontalLayout_2)
        self.pushButton = QtGui.QPushButton(self.widget)
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.verticalLayout.addWidget(self.pushButton)
        self.horizontalLayout = QtGui.QHBoxLayout()
        self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
        self.label = QtGui.QLabel(self.widget)
        self.label.setObjectName(_fromUtf8("label"))
        self.horizontalLayout.addWidget(self.label)
        self.label_2 = QtGui.QLabel(self.widget)
        self.label_2.setObjectName(_fromUtf8("label_2"))
        self.horizontalLayout.addWidget(self.label_2)
        self.verticalLayout.addLayout(self.horizontalLayout)
        NewWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(NewWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 439, 25))
        self.menubar.setObjectName(_fromUtf8("menubar"))
        NewWindow.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(NewWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        NewWindow.setStatusBar(self.statusbar)
    
        self.retranslateUi(NewWindow)
        QtCore.QMetaObject.connectSlotsByName(NewWindow)
    
      def retranslateUi(self, NewWindow):
        NewWindow.setWindowTitle(QtGui.QApplication.translate("NewWindow", "MainWindow",    None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton_2.setText(QtGui.QApplication.translate("NewWindow", "Exit", None, QtGui.QApplication.UnicodeUTF8))
        self.label_3.setText(QtGui.QApplication.translate("NewWindow", "A = ", None, QtGui.QApplication.UnicodeUTF8))
        self.lineEdit.setText(QtGui.QApplication.translate("NewWindow", "0.0", None, QtGui.QApplication.UnicodeUTF8))
        self.label_4.setText(QtGui.QApplication.translate("NewWindow", "B = ", None, QtGui.QApplication.UnicodeUTF8))
        self.lineEdit_2.setText(QtGui.QApplication.translate("NewWindow", "0.0", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton.setText(QtGui.QApplication.translate("NewWindow", "Add", None, QtGui.QApplication.UnicodeUTF8))
        self.label.setText(QtGui.QApplication.translate("NewWindow", "Answer  : ", None, QtGui.QApplication.UnicodeUTF8))
        self.label_2.setText(QtGui.QApplication.translate("NewWindow", "0.0", None, QtGui.QApplication.UnicodeUTF8))
    
    if __name__ == "__main__":
      import sys
      app = QtGui.QApplication(sys.argv)
      NewWindow = QtGui.QMainWindow()
      ui = Ui_NewWindow()
      ui.setupUi(NewWindow)
      NewWindow.show()
      sys.exit(app.exec_())
    

After running the above python program it shows the window as: ![enter image
description here](http://i.stack.imgur.com/hL388.jpg)

But I have to assign the value from the line edit to the variable A and B so I
can add them as:

    
    
    sum = A + B
    

and I have show this value `sum` in the label Answer when we clicked on push
button "Add" in above window, so please suggest proper change in above code.


Answer: You need to create a news action (`ad_values` in this case). Then, you need to
connect this action to the Buttons signal (`clicked` for example). After the
signal emits (i.e. the button is clicked), the actions reads the values from
`lineEdit` and `lineEdit_2`, adds them up and set's the text of the
`label_2`-widget (you might convert the values to int before [see the other
answer for a simple implementation])

    
    
    from PySide import QtCore, QtGui
    from PySide.QtCore import Slot
    
    try:
      _fromUtf8 = QtCore.QString.fromUtf8
    except AttributeError:
      _fromUtf8 = lambda s: s
    
    class Ui_NewWindow(object):
      def setupUi(self, NewWindow):
        NewWindow.setObjectName(_fromUtf8("NewWindow"))
        NewWindow.resize(439, 225)
        self.centralwidget = QtGui.QWidget(NewWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.pushButton_2 = QtGui.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(170, 140, 99, 27))
        self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
        self.widget = QtGui.QWidget(self.centralwidget)
        self.widget.setGeometry(QtCore.QRect(40, 30, 365, 89))
        self.widget.setObjectName(_fromUtf8("widget"))
        self.verticalLayout = QtGui.QVBoxLayout(self.widget)
        #self.verticalLayout.setMargin(0)
        self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
        self.horizontalLayout_2 = QtGui.QHBoxLayout()
        self.horizontalLayout_2.setObjectName(_fromUtf8("horizontalLayout_2"))
        self.label_3 = QtGui.QLabel(self.widget)
        self.label_3.setObjectName(_fromUtf8("label_3"))
        self.horizontalLayout_2.addWidget(self.label_3)
        self.lineEdit = QtGui.QLineEdit(self.widget)
        self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
        self.horizontalLayout_2.addWidget(self.lineEdit)
        self.label_4 = QtGui.QLabel(self.widget)
        self.label_4.setObjectName(_fromUtf8("label_4"))
        self.horizontalLayout_2.addWidget(self.label_4)
        self.lineEdit_2 = QtGui.QLineEdit(self.widget)
        self.lineEdit_2.setObjectName(_fromUtf8("lineEdit_2"))
        self.horizontalLayout_2.addWidget(self.lineEdit_2)
        self.verticalLayout.addLayout(self.horizontalLayout_2)
        self.pushButton = QtGui.QPushButton(self.widget)
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.verticalLayout.addWidget(self.pushButton)
        self.horizontalLayout = QtGui.QHBoxLayout()
        self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
        self.label = QtGui.QLabel(self.widget)
        self.label.setObjectName(_fromUtf8("label"))
        self.horizontalLayout.addWidget(self.label)
        self.label_2 = QtGui.QLabel(self.widget)
        self.label_2.setObjectName(_fromUtf8("label_2"))
        self.horizontalLayout.addWidget(self.label_2)
        self.verticalLayout.addLayout(self.horizontalLayout)
        NewWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(NewWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 439, 25))
        self.menubar.setObjectName(_fromUtf8("menubar"))
        NewWindow.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(NewWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        NewWindow.setStatusBar(self.statusbar)
    
        self.retranslateUi(NewWindow)
        QtCore.QMetaObject.connectSlotsByName(NewWindow)
    
        self.pushButton.clicked.connect(self.add_values)
    
      def retranslateUi(self, NewWindow):
        NewWindow.setWindowTitle(QtGui.QApplication.translate("NewWindow", "MainWindow",    None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton_2.setText(QtGui.QApplication.translate("NewWindow", "Exit", None, QtGui.QApplication.UnicodeUTF8))
        self.label_3.setText(QtGui.QApplication.translate("NewWindow", "A = ", None, QtGui.QApplication.UnicodeUTF8))
        self.lineEdit.setText(QtGui.QApplication.translate("NewWindow", "0.0", None, QtGui.QApplication.UnicodeUTF8))
        self.label_4.setText(QtGui.QApplication.translate("NewWindow", "B = ", None, QtGui.QApplication.UnicodeUTF8))
        self.lineEdit_2.setText(QtGui.QApplication.translate("NewWindow", "0.0", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton.setText(QtGui.QApplication.translate("NewWindow", "Add", None, QtGui.QApplication.UnicodeUTF8))
        self.label.setText(QtGui.QApplication.translate("NewWindow", "Answer  : ", None, QtGui.QApplication.UnicodeUTF8))
        self.label_2.setText(QtGui.QApplication.translate("NewWindow", "0.0", None, QtGui.QApplication.UnicodeUTF8))
    
      @Slot()
      def add_values(self):
          # you should convert the values to integers for a less error-prone code
          summed = float(self.lineEdit.text()) + float(self.lineEdit_2.text())
          self.label_2.setText(str(summed))
    
    
    if __name__ == "__main__":
      import sys
      app = QtGui.QApplication(sys.argv)
      NewWindow = QtGui.QMainWindow()
      ui = Ui_NewWindow()
      ui.setupUi(NewWindow)
      NewWindow.show()
      sys.exit(app.exec_())
    

I´m using PySide, but it should work with PyQT as well.

