我想让计算器机器人使用PyQt5和我得到的E

2024-09-28 22:09:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我想让计算器机器人使用PyQt5,我得到这个错误。 你能帮我吗??你知道吗

另外,我是PyQt5的乞丐

我的错误是:

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))

Tags: selfinitdefbutintdialoglayoutstr