我已经从 pyqt designer 设计了 ​​ui 现在我想把那个按钮和标签连接在一起

i have designed ui from pyqt designer now i want to connect that button and label together

如何定义 find Qlabel、Qpushbutton 和 QlineEdit 来修复该属性错误?

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys

from PyQt5.uic import loadUiType

Ui, _ = loadUiType('tempconverter.ui')

class MainApp(QMainWindow , Ui):
    def __init__(self):
        QMainWindow.__init__(self)
        self.setupUi(self)
        self.pushButton.clicked.connect(self.cal)

    def cal(self):
        temp = self.temp_in.text
        far=int(temp)*9/5+32
        self.answer.setText(str(far))

def main():
    app = QApplication(sys.argv)
    window = MainApp()
    window.show()
    app.exec_()

if __name__ == "__main__":
    main()

Traceback (most recent call last): File "d:/pythonproj/temp_test2.py", line 17, in cal temp = self.temp_in.text AttributeError: 'MainApp' object has no attribute 'temp_in'``

您真的应该考虑发布 .ui 文件中定义了 temp_in 的部分,但我可以看出一些错误。

首先,你有这一行:

temp = self.temp_in.text

这应该是:

temp = self.temp_in.text()

要让 QLineEdits 检索文本,您需要 运行 函数 text()(它不是参数)。

从错误消息来看,您似乎为 QLineEdit 使用了错误的名称。您是否可能不小心使用了 QLabel 的名称? 运行 pyuic5 将 .ui 文件转换为 Python 代码可能会有帮助。这将使您可以使用代码完成来查看 IDE.

中定义了哪些元素