PyQt ui2py 使用PowerShell将ui文件转为py文件并且将导入模块PyQt或PySide转换为qtpy模块开箱即用

前言

由于需要使用不同的qt环境(PySide,PyQt)所以写了这个脚本,使用找到的随便一个uic命令去转换ui文件,然后将导入模块换成qtpy这个通用库(支持pyside2-6,pyqt5-6),老版本的是Qt.py(支持pyside0-2~pyqt4-5)你可以自己改

使用

将内容保存为 ui2py.ps1 放置到后缀 ui 文件的同目录下

在拥有环境的powershell中运行 .\ui2py

powershell 复制代码
<#
.SYNOPSIS
Convert all ui files in the same directory to py files for Python to use

.DESCRIPTION
It needs to be used on terminals that have a Python environment, making sure that pyuic* or pyside*-uic is present
This script replaces the import 'from PyQt*' or 'from PySide*' in the py file with the 'from qtpy' module after compilation

.EXAMPLE
PS> .\MyScript.ps1
Run directly, no parameters required

.LINK
https://blog.csdn.net/weixin_42579717/article/details/137279306

#>
# Check whether the uic command exists
$_commands = @("pyuic5", "pyside2-uic", "pyuic6", "pyside6-uic", "pyside-uic", "pyuic")
$command = $null
foreach ($cmd in $_commands)
{
    $command = Get-Command $cmd -ErrorAction SilentlyContinue
    if ($command)
    {
        break
    }
}
if (-not$command)
{
    Write-Host "Command not found."
    exit 1
}
$command = $command.Name
Write-Output "Use command: $command"

# Gets the folder where the current script is located
$scriptDirectory = Split-Path -Parent $MyInvocation.MyCommand.Definition

# Perform conversion operations for all *.ui files in the folder
Get-ChildItem -Path $scriptDirectory -Filter *.ui | ForEach-Object {
    $uiFile = $_.FullName
    $pyFile = $_.BaseName + ".py"
    $pyFilePath = Join-Path $scriptDirectory $pyFile

    Write-Host $uiFile
    Write-Host $pyFilePath

    # Convert '.ui' to '.py'
    & $command -o $pyFilePath $uiFile

    # Check whether the file is successfully converted
    if (Test-Path $pyFilePath)
    {
        # Replace import module
        (Get-Content $pyFilePath -Encoding UTF8) -replace "from (PyQt[456]?|PySide[26]?)", "from qtpy" | Set-Content $pyFilePath -Encoding UTF8
        Write-Host "Files have been converted and modified successfully: $pyFile"
    }
    else
    {
        Write-Host "convert failed: $uiFile"
    }
}

如果你的项目是有结构的比如下图

需要将ui转出的py文件放置到上层的ui目录中的话,可以修改以下内容

添加父文件夹$parentDirectory = Split-Path -Parent $scriptDirectory

powershell 复制代码
# Gets the folder where the current script is located
$scriptDirectory = Split-Path -Parent $MyInvocation.MyCommand.Definition
$parentDirectory = Split-Path -Parent $scriptDirectory

# Perform conversion operations for all *.ui files in the folder
Get-ChildItem -Path $scriptDirectory -Filter *.ui | ForEach-Object {
    $uiFile = $_.FullName
    $pyFile = $_.BaseName + ".py"
    $pyFilePath = Join-Path $parentDirectory ("ui/" + $pyFile)

PS

  • qrc转py同理 可以看看另一篇文章 qrc2py
相关推荐
黄公子学安全39 分钟前
Java的基础概念(一)
java·开发语言·python
程序员一诺1 小时前
【Python使用】嘿马python高级进阶全体系教程第10篇:静态Web服务器-返回固定页面数据,1. 开发自己的静态Web服务器【附代码文档】
后端·python
小木_.2 小时前
【Python 图片下载器】一款专门为爬虫制作的图片下载器,多线程下载,速度快,支持续传/图片缩放/图片压缩/图片转换
爬虫·python·学习·分享·批量下载·图片下载器
Jiude2 小时前
算法题题解记录——双变量问题的 “枚举右,维护左”
python·算法·面试
唐小旭2 小时前
python3.6搭建pytorch环境
人工智能·pytorch·python
是十一月末3 小时前
Opencv之对图片的处理和运算
人工智能·python·opencv·计算机视觉
爱学测试的李木子3 小时前
Python自动化测试的2种思路
开发语言·软件测试·python
kitsch0x973 小时前
工具学习_Conan 安装第三方库
开发语言·python·学习
梦幻精灵_cq4 小时前
《点点之歌》“意外”诞生记
python
张狂年少4 小时前
电力通信规约-104实战
java·开发语言·python