我已经从pyqt designer设计了ui,现在我想将按钮和标签连接在一起

2024-04-25 23:42:03 发布

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

如何定义findqlabel、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'``


1条回答
网友
1楼 · 发布于 2024-04-25 23:42:03

您确实应该考虑从定义了TwittIn的.UI文件中发布部分,但我可以看到一些错误。p>

首先,你有这一行:

temp = self.temp_in.text

这应该是:

temp = self.temp_in.text()

要使用QLineEdits检索文本,需要运行函数text()(它不是参数)

从错误消息中,您似乎使用了错误的QLineEdit名称。是否可能您无意中使用了QLabel的名称?可能有用的是运行pyuic5将.ui文件转换为Python代码。这将允许您使用代码完成来查看IDE中定义了哪些元素

相关问题 更多 >

    热门问题