当前位置: 首页 > news >正文

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

<#
.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 SilentlyContinueif ($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 $pyFileWrite-Host $uiFileWrite-Host $pyFilePath# Convert '.ui' to '.py'& $command -o $pyFilePath $uiFile# Check whether the file is successfully convertedif (Test-Path $pyFilePath){# Replace import module(Get-Content $pyFilePath -Encoding UTF8) -replace "from (PyQt[456]?|PySide[26]?)", "from qtpy" | Set-Content $pyFilePath -Encoding UTF8Write-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
http://www.lryc.cn/news/330682.html

相关文章:

  • javascript中的浅拷贝和深拷贝
  • vue 实现自定义分页打印 window.print
  • 基于 Erlang 的随机账户分配机制
  • 数码论坛系统的设计与实现|Springboot+ Mysql+Java+ B/S结构(可运行源码+数据库+设计文档)电子科技数码爱好者交流信息新闻畅聊讨论评价
  • 时序预测 | Matlab实现CPO-LSTM【24年新算法】冠豪猪优化长短期记忆神经网络时间序列预测
  • 探索设计模式的魅力:AI大模型如何赋能C/S模式,开创服务新纪元
  • 2024年NAND价格市场继续上涨
  • 分布式算法 - ZAB算法
  • Java设计之道:色即是空,空即是色
  • 深度学习:基于PyTorch的模型解释工具Captum
  • 公司官网怎么才会被百度收录
  • 机器学习模型——SVM(支持向量机)
  • 服务器CPU使用过高的原因
  • 基于tensorflow和kereas的孪生网络推理图片相似性
  • day4|gin的中间件和路由分组
  • nodejs的express负载均衡
  • 计算机网络-HTTP相关知识-RSA和ECDHE及优化
  • axios 封装 http 请求详解
  • 牛客2024年愚人节比赛(A-K)
  • loadbalancer 引入与使用
  • Yolov5封装detect.py面向对象
  • 入门级深度学习主机组装过程
  • python爬虫之selenium4使用(万字讲解)
  • 【ARM 嵌入式 C 头文件系列 22 -- 头文件 stdint.h 介绍】
  • LabVIEW专栏三、探针和断点
  • Transformer模型-softmax的简明介绍
  • 记录一下做工厂的打印pdf程序
  • Linux网络编程一(协议、TCP协议、UDP、socket编程、TCP服务器端及客户端)
  • Python读取Excel根据每行信息生成一个PDF——并自定义添加文本,可用于制作准考证
  • http: server gave HTTP response to HTTPS client 分析一下这个问题如何解决中文告诉我详细的解决方案