计算PyQt中的两个值

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

我正在玩PyQt,并尝试将两个值(数字)加在一起。当我尝试显示此输出时,我仅收到此消息:

这里的图片“图片在这里”

我的代码:

        labelwidth = QLabel('Width in meter', self)
        self.width = QLineEdit(self)
        self.width.move (100,0)
        labelwidth.move (5,0)


        labeldepth = QLabel('Depth in meter', self)
        self.depth = QLineEdit(self)
        self.depth.move (100,50)
        labeldepth.move (5,50)

        #Send data
        btn = QPushButton('Send', self)
        btn.clicked.connect(self.send_data)
        btn.move (100, 100)

        self.show()

    def send_data(self):
        width_to_str = str(self.width)
        dept_to_str = str(self.depth)
        kvm = width_to_int + dept_to_int
        labelkvm = QLabel(kvm, self)
        labelkvm.move = (200, 100)
        QMessageBox.about(self, "Sendt", kvm)
        self.show()

在汇总数字之前,我曾尝试将其转换为int。

我该如何解决?

python pyqt
1个回答
0
投票

尝试:

import sys
import pandas as pd
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *


class MainWindow(QWidget):
    def __init__(self):
        super().__init__()

        labelwidth = QLabel('Width in meter', self)
        self.width = QLineEdit(self)
        self.width.move (100,0)
        labelwidth.move (5,0)


        labeldepth = QLabel('Depth in meter', self)
        self.depth = QLineEdit(self)
        self.depth.move (100,50)
        labeldepth.move (5,50)

        #Send data
        btn = QPushButton('Send', self)
        btn.clicked.connect(self.send_data)
        btn.move (100, 100)

        self.show()

    def send_data(self):
        width_to_str = int(self.width.text())     # <---
        dept_to_str  = int(self.depth.text())     # <---
        kvm = str(width_to_str + dept_to_str)     # <---
        labelkvm = QLabel(kvm, self)
        labelkvm.move = (200, 100)
        QMessageBox.about(self, "Sendt", kvm)
        self.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    mainwindow = MainWindow()
    mainwindow.show()
    sys.exit(app.exec_())        

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.