[PyQT5]初体验-编译ui文件-自定义槽函数
- 2020 年 3 月 10 日
- 筆記
原创文章,欢迎转载。转载请注明:转载自 祥的博客
原文链接:https://blog.csdn.net/humanking7/article/details/80403189
- Step1. 用Qt Designer设计一个想要的UI
- Step2. 用pyuic5编译为py文件
- Step3. 继承一个dlg类
- Step4. show之
Step1. 用Qt Designer设计一个想要的UI

Step2. 用pyuic5编译为py文件
运行cmd
执行命令
> pyuic5 dlg_UI.ui > dlg_UI.py # 也有下面这样的方式 {没有测试过} > pyuic5 -o dlg_UI.py dlg_UI.ui # 其中,-o [输出文件的名称] [生成的ui文件的名称]
此时我们得到了dlg_UI.py
文件
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'dlg_UI.ui' # # Created by: PyQt5 UI code generator 5.10.1 # # WARNING! All changes made in this file will be lost! from PyQt5 import QtCore, QtGui, QtWidgets class Ui_Dialog(object): def setupUi(self, Dialog): Dialog.setObjectName("Dialog") Dialog.resize(740, 294) self.tableWidget = QtWidgets.QTableWidget(Dialog) self.tableWidget.setGeometry(QtCore.QRect(50, 81, 601, 171)) self.tableWidget.setObjectName("tableWidget") self.tableWidget.setColumnCount(4) self.tableWidget.setRowCount(0) item = QtWidgets.QTableWidgetItem() self.tableWidget.setHorizontalHeaderItem(0, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setHorizontalHeaderItem(1, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setHorizontalHeaderItem(2, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setHorizontalHeaderItem(3, item) self.btn_GetFile = QtWidgets.QPushButton(Dialog) self.btn_GetFile.setGeometry(QtCore.QRect(330, 20, 75, 23)) self.btn_GetFile.setObjectName("btn_GetFile") self.edit_dir = QtWidgets.QLineEdit(Dialog) self.edit_dir.setGeometry(QtCore.QRect(50, 20, 271, 20)) self.edit_dir.setReadOnly(True) self.edit_dir.setObjectName("edit_dir") self.retranslateUi(Dialog) QtCore.QMetaObject.connectSlotsByName(Dialog) def retranslateUi(self, Dialog): _translate = QtCore.QCoreApplication.translate Dialog.setWindowTitle(_translate("Dialog", "Dialog")) item = self.tableWidget.horizontalHeaderItem(0) item.setText(_translate("Dialog", "选择")) item = self.tableWidget.horizontalHeaderItem(1) item.setText(_translate("Dialog", "路径")) item = self.tableWidget.horizontalHeaderItem(2) item.setText(_translate("Dialog", "文件编码")) item = self.tableWidget.horizontalHeaderItem(3) item.setText(_translate("Dialog", "转换编码")) self.btn_GetFile.setText(_translate("Dialog", "获取文件"))
Step3. 继承一个dlg类
from dlg_UI import * from PyQt5.QtCore import * from PyQt5.QtWidgets import * class listDlg(QDialog): def __init__(self, name="Dlg"): # 构造函数 super().__init__() self.initUI(name) self.cnt_tmp = 0 def initUI(self, name): # 初始化函数 self.ui = Ui_Dialog() self.ui.setupUi(self) self.setWindowTitle(name) self.initConnect() def initConnect(self): # 初始化信号与槽 self.ui.btn_GetFile.clicked.connect(self.slot_btn_getFile) pass def setDirEditText(self, str_show): # 更新edit的文本 self.ui.edit_dir.setText( str_show ) # ###################### # 槽函数 # ###################### def slot_btn_getFile(self): #btn_getFile的槽函数 self.cnt_tmp = self.cnt_tmp + 1 self.setDirEditText(str(self.cnt_tmp)) pass
Step4. show之
这就是主函数了
from listDlg import * from PyQt5.QtCore import * from PyQt5.QtWidgets import * import sys if __name__ == '__main__': app = QApplication( sys.argv ) dlg = listDlg('转换文件编码') dlg.show() sys.exit(app.exec_()) pass
