我想使用PyQt5制作计算器机器人,我收到错误

时间:2017-12-31 09:07:32

标签: python pyqt5 typeerror python-3.6

我想使用PyQt5制作计算器机器人我收到此错误。 你可以帮我吗??

P.S我是PyQt5中的Begginer

我的错误是这个:

TypeError: setText(self, str): argument 1 has unexpected type 'int'

我的代码是这样的:

class Dialog(QDialog):
    def __init__(self):
        QDialog.__init__(self)
        self.dialog = QComboBox()
        self.lbl = QLabel("Choose Gas Name:")
        self.but = QPushButton("Calculate")
        self.litre = QLineEdit(self)
        self.regular = QLabel("Regular >>> "+str(2.27))
        self.euro_reg = QLabel("Euro Regular >>> "+str(2.33))
        self.diesel = QLabel("Diesel >>> "+str(2.39))
        self.calculated = QLabel("")
        self.init_ui()
    def init_ui(self):
        layout = QVBoxLayout()
        layout.addWidget(self.regular)
        layout.addWidget(self.euro_reg)
        layout.addWidget(self.diesel)
        layout.addWidget(self.litre)
        layout.addWidget(self.lbl)
        layout.addWidget(self.dialog)
        layout.addWidget(self.but)
        layout.addWidget(self.calculated)

        self.dialog.addItem("Regular")
        self.dialog.addItem("Euro Regular")
        self.dialog.addItem("Diesel")
        self.setGeometry(100,100,200,200)
        self.but.clicked.connect(self.calculate)
        self.setLayout(layout)
        self.show()
    def calculate(self, layout):
        if self.litre.text() == "":
            self.calculated.setText("<font color=red>Please Enter Litre")
        else:
            litre_int = int(self.litre.text())
            self.calculated.setText(litre_int*int(2.27))

1 个答案:

答案 0 :(得分:1)

setText需要一个字符串,而不是一个int。您需要将结果显式转换为字符串:

self.calculated.setText(str(litre_int*int(2.27)))
# Here -----------------^