gpt4 book ai didi

python - 我想使用 PyQt5 制作计算器机器人,但出现错误

转载 作者:太空宇宙 更新时间:2023-11-03 14:19:21 25 4
gpt4 key购买 nike

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

P.S 我是 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))

最佳答案

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

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

关于python - 我想使用 PyQt5 制作计算器机器人,但出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48041277/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com