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

本文介绍了一个脚本,用于在具有Python环境的终端中自动将不同Qt版本(PySide、PyQt)的UI文件转换为qtpy兼容的Python代码,同时替换导入模块,适用于PySide2-6、PyQt5-6等环境。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

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

使用

将内容保存为 ui2py.ps1 放置到后缀 ui 文件的同目录下
在拥有环境的powershell中运行 .\ui2py

<#
.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


# 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值