zoukankan      html  css  js  c++  java
  • 每日一程-3. PyQt5-通过Python脚本把当前目录下的所有.ui文件转换为.py文件

    • 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"))
    
  • 相关阅读:
    React篇-子组件调用父组件方法,并传值
    RN-ios模拟器上调出中文输入法
    mac-破解2018 webstorm
    React篇-滚动条下移的触发在react的生命周期分析
    javascript篇-typeof,instanceof,constructor,toString判断数据类型的用法和区别
    javascript篇-slice(),splice(),split(),substring(),substr()的用法以及区别
    javascript篇-console.log()打印object却显示为字符串[object object]
    Linux的几种关机命令
    深入浅出Oracle:DBA入门、进阶与诊断案例 PDF 下载
    SQL state [72000]; error code [1461]; ORA-01461: 仅能绑定要插入 LONG 列的 LONG 值 ; nested exception is java.sql.BatchUpdateException: ORA-01461: 仅能绑定要插入 LONG 列的 LONG 值
  • 原文地址:https://www.cnblogs.com/leo1875/p/10358620.html
Copyright © 2011-2022 走看看