I need to get a number from my QLineEdit in the GUI and use it in a calculation and display the result in a message box but i keep getting errors
self.connect(self.calculate, SIGNAL("clicked()"),self.showMessageBox)
y = int(self.input1.get())
x = 31 + y
def showMessageBox(self):
QMessageBox.information(self,"NRC","You need " + str(x))
Use self.input1.text()
to read the current text content of the widget.
Also note that Python will forget about x
when the first method ends, so x
will be unknown in showMessageBox()
Related:
There are several problems with the way you've structured your code. It's probably easiest to just show you a working example that meets your spec, so you can see how it all goes together:
from PyQt4 import QtCore, QtGui
class Window(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.input1 = QtGui.QLineEdit(self)
self.calculate = QtGui.QPushButton('Calculate', self)
self.calculate.clicked.connect(self.handleCalculate)
layout = QtGui.QHBoxLayout(self)
layout.addWidget(self.input1)
layout.addWidget(self.calculate)
def handleCalculate(self):
y = int(self.input1.text())
x = 31 + y
self.showMessageBox(x)
def showMessageBox(self, value):
QtGui.QMessageBox.information(self, 'NRC', 'You need %s' % value)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.setGeometry(500, 300, 300, 50)
window.show()
sys.exit(app.exec_())
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.