- Author: Notus(hehe_xiao@qq.com)
- Create: 2019-02-10
- Update: 2019-02-16
PyQt5-通过Python脚本把当前目录下的所有.ui文件转换为.py文件
环境
操作系统: Windows 10 专业版 64-bit (10.0, Build 16299) (16299.rs3_release.170928-1534)
Python Version: 3.7.1
PyQt5 Version: 5.11.3
Qt Designer Version: 5.11.2
python脚本代码如下(将当前目录下的所有.ui文件转为.py文件)
\'\'\'
通过Python脚本把.ui文件转换为.py文件
@Author: Notus(hehe_xiao@qq.com)
@Create: 2019-02-10
@Update: 2019-02-10
\'\'\'
import os
import os.path
# UI 文件所在的路径
dir = \'./\'
# 列出目录下的所有 UI 文件
def listUiFile():
list = []
files = os.listdir(dir)
for filename in files:
#print(dir + os.sep + f)
#print(filename)
if os.path.splitext(filename)[1] == \'.ui\':
list.append(filename)
return list
# 把扩展名为.ui的成扩展名为.py的文件
def transPyFile(filename):
return os.path.splitext(filename)[0] + \'.py\'
# 调用系统命令把 UI 文件改成扩展名为 Python 文件
def runMain():
list = listUiFile()
for uifile in list:
pyfile = transPyFile(uifile)
cmd = \'pyuic5 -o {pyfile} {uifile}\'.format(pyfile=pyfile, uifile=uifile)
#print(cmd)
os.system(cmd)
# 程序的入口
if __name__ == \'__main__\':
runMain()
窗体截图如下
原.ui文件内容如下
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>330</x>
<y>200</y>
<width>91</width>
<height>51</height>
</rect>
</property>
<property name="text">
<string>PushButton</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>23</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
转换后的 .py 文件如下
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file \'a.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_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(330, 200, 91, 51))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))