How to read text boxes, and create action from the button in PyQt with a form created in the Designer?

Boas,

I drew a widget in Qt Desginer, and converted to python, and using the tutorial, I can run the widget (two textbox, 1 Button and a label). The idea is to add the contents of the boxes, press the button and display the result on a label.

However, I can only run the widget, I don't know how to read the text boxes or how to associate code when clicking the button.

The code that runs the widget is as follows:

from PyQt5 import uic
from PyQt5.QtWidgets import QApplication


Form, Window, Button = uic.loadUiType("PYQT_TUTORIAL.ui")

app = QApplication([])
window = Window()
form = Form()


form.setupUi(window)

window.show()
app.exec_()

Thank you for help, greetings.

Author: Daniel Mendes, 2020-04-23

1 answers

You will have to create a function to be able to call when the button is clicked ex:

# função ler a textbox e realiza a soma
def soma(self):
    valor1 = self.textbox1.Text()
    valor2 = self.textbox2.Text()
    resultado = valor1 + valor2
    self.label.setText(resultado)

# chama a função quando o botão for clicado
self.botao.clicked.connect(self.soma)
 0
Author: Ailson, 2020-05-23 21:40:25