如何从事件函数返回值并将其设置为LineEdit?

问题描述 投票:-1回答:1

我正在努力地返回一个值并在窗口中显示它。我可以打印该值。我想把返回值设置为Line3。我想做一个基本的程序,在第1行和第2行有两个输入值,输出或这两个值的和将显示在第3行。

import sys
from PyQt5.QtWidgets import QApplication,QWidget,QLabel, QLineEdit, QPushButton

def dialog(): *#this block will add two input from line1 and line2* 
    value1=int(line1.text())
    value2=int(line2.text())
    result=value1+value2
    print(result)
    return result

if __name__=="__main__":
    app=QApplication(sys.argv)
    w=QWidget()
    w.resize(400,200)
    w.setWindowTitle('Calculator')
    label=QLabel(w)
    label.setText('Hi ! Enter the inputs')
    label.move(50, 50)
    label1=QLabel(w)
    label1.setText('Result:')
    label1.move(85,150)

    line1=QLineEdit(w)
    line2=QLineEdit(w)
    line1.move(50,70)
    line2.move(200,70)
    line3=QLineEdit(w)
    line3.move(130, 150)
    line3.setText('0')

    btn1=QPushButton(w)
    btn1.setText('Submit')
    btn1.move(150, 100)
    btn1.clicked.connect(dialog)

    **#now I want to set the return value from dialog to line3**

    w.show()
    sys.exit(app.exec_())
python pyqt pyqt4
1个回答
0
投票
class MyWidget(QWidget):
    def __init__(self, parent=None):
        super(QWidget, self).__init__(parent)
        self.resize(400, 200)
        self.setWindowTitle('Calculator')

        self.line1 = QLineEdit(self)
        self.line2 = QLineEdit(self)
        self.line1.move(50,70)
        self.line2.move(200,70)
        self.line3 = QLineEdit(self)
        self.line3.move(130, 150)
        self.line3.setText('0')

        btn1 = QPushButton(self)
        btn1.setText('Submit')
        btn1.move(150, 100)
        btn1.clicked.connect(self.dialog)

    def dialog(self):
        value1 = int(self.line1.text())
        value2 = int(self.line2.text())
        result = value1 + value2
        self.line3.setText(str(result))
© www.soinside.com 2019 - 2024. All rights reserved.