I know the basics and the whole structure of calculations in Python. I decided to take a bigger step and create a graphical interface for these calculations. Well, creating the interface in QT DESIGNER and then converting to Python is easy, the problem is in making sense of this layout. QLineEdit, QSpinBox, QDoubleSpinBox, QTextEdit blablabla.
What have I already achieved? Match 2 results and enter another box. But that's not what I want. What I want is to calculate. I have already tried converting the results to float or int and without result.
Follow interface code:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'firstgui.ui'
#
# Created by: PyQt5 UI code generator 5.7
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_myfirstgui(object):
def setupUi(self, myfirstgui):
myfirstgui.setObjectName("myfirstgui")
myfirstgui.resize(411, 247)
self.buttonBox = QtWidgets.QDialogButtonBox(myfirstgui)
self.buttonBox.setGeometry(QtCore.QRect(20, 210, 381, 32))
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Close)
self.buttonBox.setObjectName("buttonBox")
self.myTextInput = QtWidgets.QDoubleSpinBox(myfirstgui)
self.myTextInput.setGeometry(QtCore.QRect(10, 10, 101, 21))
self.myTextInput.setButtonSymbols(QtWidgets.QAbstractSpinBox.NoButtons)
self.myTextInput.setObjectName("myTextInput")
self.clearBtn = QtWidgets.QPushButton(myfirstgui)
self.clearBtn.setGeometry(QtCore.QRect(10, 180, 101, 23))
self.clearBtn.setObjectName("clearBtn")
self.addBtn = QtWidgets.QPushButton(myfirstgui)
self.addBtn.setGeometry(QtCore.QRect(10, 110, 101, 23))
self.addBtn.setObjectName("addBtn")
self.myTextInput_2 = QtWidgets.QDoubleSpinBox(myfirstgui)
self.myTextInput_2.setGeometry(QtCore.QRect(10, 40, 101, 21))
self.myTextInput_2.setButtonSymbols(QtWidgets.QAbstractSpinBox.NoButtons)
self.myTextInput_2.setObjectName("myTextInput_2")
self.plainTextEdit = QtWidgets.QPlainTextEdit(myfirstgui)
self.plainTextEdit.setGeometry(QtCore.QRect(120, 10, 281, 191))
self.plainTextEdit.setObjectName("plainTextEdit")
self.retranslateUi(myfirstgui)
self.buttonBox.accepted.connect(myfirstgui.accept)
self.buttonBox.rejected.connect(myfirstgui.reject)
QtCore.QMetaObject.connectSlotsByName(myfirstgui)
def retranslateUi(self, myfirstgui):
_translate = QtCore.QCoreApplication.translate
myfirstgui.setWindowTitle(_translate("myfirstgui", "My First Gui!"))
self.clearBtn.setText(_translate("myfirstgui", "clear"))
self.addBtn.setText(_translate("myfirstgui", "add"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
myfirstgui = QtWidgets.QDialog()
ui = Ui_myfirstgui()
ui.setupUi(myfirstgui)
myfirstgui.show()
sys.exit(app.exec_())
Program code:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from firstgui import Ui_myfirstgui
class MyFirstGuiProgram(Ui_myfirstgui):
def __init__(self, dialog):
Ui_myfirstgui.__init__(self)
self.setupUi(dialog)
# Connect "add" button with a custom function (addInputTextToListbox)
self.addBtn.clicked.connect(self.addInputTextToListbox)
def addInputTextToListbox(self):
soma = (self.myTextInput.text()) + (self.myTextInput_2.text())
self.plainTextEdit.appendPlainText(str(soma))
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
dialog = QtWidgets.QDialog()
prog = MyFirstGuiProgram(dialog)
dialog.show()
sys.exit(app.exec_())
My opinion is that this is no longer python ...
Attempts that did not return:
def addInputTextToListbox(self):
soma = (self.myTextInput.text().toInt()) + (self.myTextInput_2.text().toInt())
self.plainTextEdit.appendPlainText(str(soma))
and
def addInputTextToListbox(self):
soma = int(self.myTextInput.text()) + int(self.myTextInput_2.text())
self.plainTextEdit.appendPlainText(str(soma))