Pyqt5 designer制作的.ui 转化为.py 怎么直接使用

.ui界面是:

在这里插入图片描述

生成的.py是:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'html_to_doors.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Mainform(object):
    def setupUi(self, Mainform):
        Mainform.setObjectName("Mainform")
        Mainform.resize(1430, 817)
        self.Title = QtWidgets.QLabel(Mainform)
        self.Title.setGeometry(QtCore.QRect(380, 20, 751, 30))
        font = QtGui.QFont()
        font.setFamily("仿宋")
        font.setPointSize(18)
        font.setBold(True)
        font.setItalic(False)
        font.setWeight(75)
        self.Title.setFont(font)
        self.Title.setObjectName("Title")
        self.openfile = QtWidgets.QPushButton(Mainform)
        self.openfile.setGeometry(QtCore.QRect(1200, 90, 121, 33))
        font = QtGui.QFont()
        font.setPointSize(12)
        self.openfile.setFont(font)
        self.openfile.setObjectName("openfile")
        self.filepath = QtWidgets.QLineEdit(Mainform)
        self.filepath.setGeometry(QtCore.QRect(40, 90, 1121, 31))
        self.filepath.setObjectName("filepath")
        self.doors_text = QtWidgets.QTextEdit(Mainform)
        self.doors_text.setGeometry(QtCore.QRect(30, 240, 661, 531))
        self.doors_text.setObjectName("doors_text")
        self.version = QtWidgets.QLabel(Mainform)
        self.version.setGeometry(QtCore.QRect(1060, 40, 55, 16))
        font = QtGui.QFont()
        font.setPointSize(10)
        font.setItalic(True)
        self.version.setFont(font)
        self.version.setObjectName("version")
        self.capl_text = QtWidgets.QTextEdit(Mainform)
        self.capl_text.setGeometry(QtCore.QRect(730, 240, 661, 531))
        self.capl_text.setObjectName("capl_text")
        self.start = QtWidgets.QPushButton(Mainform)
        self.start.setGeometry(QtCore.QRect(1200, 150, 121, 51))
        font = QtGui.QFont()
        font.setPointSize(12)
        self.start.setFont(font)
        self.start.setObjectName("start")
        self.doors = QtWidgets.QLabel(Mainform)
        self.doors.setGeometry(QtCore.QRect(50, 200, 551, 31))
        font = QtGui.QFont()
        font.setFamily("仿宋")
        font.setPointSize(18)
        font.setBold(True)
        font.setItalic(False)
        font.setWeight(75)
        self.doors.setFont(font)
        self.doors.setObjectName("doors")
        self.capl = QtWidgets.QLabel(Mainform)
        self.capl.setGeometry(QtCore.QRect(740, 200, 371, 30))
        font = QtGui.QFont()
        font.setFamily("仿宋")
        font.setPointSize(18)
        font.setBold(True)
        font.setItalic(False)
        font.setWeight(75)
        self.capl.setFont(font)
        self.capl.setObjectName("capl")
        self.bottom = QtWidgets.QLabel(Mainform)
        self.bottom.setGeometry(QtCore.QRect(640, 780, 161, 30))
        font = QtGui.QFont()
        font.setFamily("MS Shell Dlg 2")
        font.setPointSize(10)
        font.setBold(False)
        font.setItalic(False)
        font.setWeight(50)
        self.bottom.setFont(font)
        self.bottom.setObjectName("bottom")

        self.retranslateUi(Mainform)
        QtCore.QMetaObject.connectSlotsByName(Mainform)

    def retranslateUi(self, Mainform):
        _translate = QtCore.QCoreApplication.translate
        Mainform.setWindowTitle(_translate("Mainform", "Dialog"))
        self.Title.setText(_translate("Mainform", "HTML Steps to Doors Test Specification Tool"))
        self.openfile.setText(_translate("Mainform", "OpenFile"))
        self.version.setText(_translate("Mainform", "V1.0"))
        self.start.setText(_translate("Mainform", "Start"))
        self.doors.setText(_translate("Mainform", "Doors Test Specification:"))
        self.capl.setText(_translate("Mainform", "Capl Test Purpose:"))
        self.bottom.setText(_translate("Mainform", "©2020 veoneer"))


更改后为:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'html_to_doors.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets
import sys
from PyQt5.QtWidgets import QApplication
class MainWindow(QtWidgets.QMainWindow):

    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        self.setupUi(self)
        self.retranslateUi(self)

    def setupUi(self, Mainform):
        Mainform.setObjectName("MainWindow")
        Mainform.resize(1430, 817)
        self.Title = QtWidgets.QLabel(Mainform)
        self.Title.setGeometry(QtCore.QRect(380, 20, 751, 30))
        font = QtGui.QFont()
        font.setFamily("仿宋")
        font.setPointSize(18)
        font.setBold(True)
        font.setItalic(False)
        font.setWeight(75)
        self.Title.setFont(font)
        self.Title.setObjectName("Title")
        self.openfile = QtWidgets.QPushButton(Mainform)
        self.openfile.setGeometry(QtCore.QRect(1200, 90, 121, 33))
        font = QtGui.QFont()
        font.setPointSize(12)
        self.openfile.setFont(font)
        self.openfile.setObjectName("openfile")
        self.filepath = QtWidgets.QLineEdit(Mainform)
        self.filepath.setGeometry(QtCore.QRect(40, 90, 1121, 31))
        self.filepath.setObjectName("filepath")
        self.doors_text = QtWidgets.QTextEdit(Mainform)
        self.doors_text.setGeometry(QtCore.QRect(30, 240, 661, 531))
        self.doors_text.setObjectName("doors_text")
        self.version = QtWidgets.QLabel(Mainform)
        self.version.setGeometry(QtCore.QRect(1060, 40, 55, 16))
        font = QtGui.QFont()
        font.setPointSize(10)
        font.setItalic(True)
        self.version.setFont(font)
        self.version.setObjectName("version")
        self.capl_text = QtWidgets.QTextEdit(Mainform)
        self.capl_text.setGeometry(QtCore.QRect(730, 240, 661, 531))
        self.capl_text.setObjectName("capl_text")
        self.start = QtWidgets.QPushButton(Mainform)
        self.start.setGeometry(QtCore.QRect(1200, 150, 121, 51))
        font = QtGui.QFont()
        font.setPointSize(12)
        self.start.setFont(font)
        self.start.setObjectName("start")
        self.doors = QtWidgets.QLabel(Mainform)
        self.doors.setGeometry(QtCore.QRect(50, 200, 551, 31))
        font = QtGui.QFont()
        font.setFamily("仿宋")
        font.setPointSize(18)
        font.setBold(True)
        font.setItalic(False)
        font.setWeight(75)
        self.doors.setFont(font)
        self.doors.setObjectName("doors")
        self.capl = QtWidgets.QLabel(Mainform)
        self.capl.setGeometry(QtCore.QRect(740, 200, 371, 30))
        font = QtGui.QFont()
        font.setFamily("仿宋")
        font.setPointSize(18)
        font.setBold(True)
        font.setItalic(False)
        font.setWeight(75)
        self.capl.setFont(font)
        self.capl.setObjectName("capl")
        self.bottom = QtWidgets.QLabel(Mainform)
        self.bottom.setGeometry(QtCore.QRect(640, 780, 161, 30))
        font = QtGui.QFont()
        font.setFamily("MS Shell Dlg 2")
        font.setPointSize(10)
        font.setBold(False)
        font.setItalic(False)
        font.setWeight(50)
        self.bottom.setFont(font)
        self.bottom.setObjectName("bottom")

        self.retranslateUi(Mainform)
        QtCore.QMetaObject.connectSlotsByName(Mainform)

    def retranslateUi(self, Mainform):
        _translate = QtCore.QCoreApplication.translate
        Mainform.setWindowTitle(_translate("Mainform", "Dialog"))
        self.Title.setText(_translate("Mainform", "HTML Steps to Doors Test Specification Tool"))
        self.openfile.setText(_translate("Mainform", "OpenFile"))
        self.version.setText(_translate("Mainform", "V1.0"))
        self.start.setText(_translate("Mainform", "Start"))
        self.doors.setText(_translate("Mainform", "Doors Test Specification:"))
        self.capl.setText(_translate("Mainform", "Capl Test Purpose:"))
        self.bottom.setText(_translate("Mainform", "©2020 veoneer"))

if __name__ == '__main__':
    app=QApplication(sys.argv)
    ex=MainWindow()
    ex.show()
    sys.exit(app.exec_())

修改的地方是:

import sys
from PyQt5.QtWidgets import QApplication
class MainWindow(QtWidgets.QMainWindow):

    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        self.setupUi(self)
        self.retranslateUi(self)

if __name__ == '__main__':
    app=QApplication(sys.argv)
    ex=MainWindow()
    ex.show()
    sys.exit(app.exec_())
好的,我会尽力回答您的问题。 首先,您需要安装PyQt5,可以使用以下命令进行安装: ``` pip install PyQt5 ``` 然后,您需要打开designer.exe进行UI设计。可以通过以下命令打开designer.exe: ``` pyqt5-tools designer ``` 接下来,按照以下步骤进行设计: 1. 选择“Main Window”模板。 2. 在窗口中添加一个“QLineEdit”组件作为显示区域,设置其为只读。 3. 在窗口中添加多个“QPushButton”组件作为数字和操作符按钮,设置其文本为相应的数字和操作符。 4. 在窗口中添加一个“QPushButton”组件作为“清空”按钮,设置其文本为“C”。 5. 在窗口中添加一个“QPushButton”组件作为“等于”按钮,设置其文本为“=”。 6. 将所有按钮的大小和位置进行调整,使其更加美观。 完成UI设计后,保存UI文件并使用以下命令将其换为Python代码: ``` pyuic5 -o calculator.py calculator.ui ``` 最后,编写Python代码实现计算器的基本功能。可以使用以下代码实现: ```python from PyQt5.QtWidgets import QApplication, QMainWindow from PyQt5 import uic class Calculator(QMainWindow): def __init__(self): super().__init__() uic.loadUi('calculator.ui', self) self.clearButton.clicked.connect(self.clearDisplay) self.equalButton.clicked.connect(self.calculateResult) self.numberButtons = [self.number0Button, self.number1Button, self.number2Button, self.number3Button, self.number4Button, self.number5Button, self.number6Button, self.number7Button, self.number8Button, self.number9Button] self.operatorButtons = [self.plusButton, self.minusButton, self.multiplyButton, self.divideButton] for button in self.numberButtons: button.clicked.connect(self.appendNumber) for button in self.operatorButtons: button.clicked.connect(self.appendOperator) self.display = '' self.operator = '' self.result = 0 def clearDisplay(self): self.display = '' self.operator = '' self.result = 0 self.displayLineEdit.setText('0') def appendNumber(self): button = self.sender() number = button.text() self.display += number self.displayLineEdit.setText(self.display) def appendOperator(self): button = self.sender() self.operator = button.text() self.result = int(self.display) self.display = '' def calculateResult(self): if self.operator == '+': self.result += int(self.display) elif self.operator == '-': self.result -= int(self.display) elif self.operator == '*': self.result *= int(self.display) elif self.operator == '/': self.result /= int(self.display) self.displayLineEdit.setText(str(self.result)) if __name__ == '__main__': app = QApplication([]) calculator = Calculator() calculator.show() app.exec_() ``` 这个代码实现了一个简单的计算器,可以进行加、减、乘、除四种运算。当然,您也可以根据自己的需要进行扩展。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蚂蚁小兵

慢慢长夜磨一章好文章,费烟!!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值