参考文章:
pyqt5迁移pysd6问题记录_pyqt5的代码转为pyside6-CSDN博客
Python 图形界面框架 PySide6 使用及避坑指南-CSDN博客
- 安装pyside6
- 把PyQt5全部替换为
PySide6
pyqtSignal()全部
替换为Signal()
- 把ui2py的 pyuic5.exe 改为 pyside6-uic.exe
- 把 QRegExpValidator、QRegExp,全部替换为 QRegularExpressionValidator、QRegularExpression
自适应pyqt5和pyside6的方式
(这里假定之前大面积使用pyqt5,现需要切换pyside6)
file: pyside_check.py
HAVE_PYSIDE6 = False
try:
from PySide6 import *
HAVE_PYSIDE6 = True
except ImportError:
HAVE_PYSIDE6 = False
要修改的文件:
import pyside_check
if pyside_check.HAVE_PYSIDE6:
from PySide6.QtCore import Signal as pyqtSignal
from PySide6.QtCore import QRegularExpression as QRegExp
from PySide6.QtGui import QRegularExpressionValidator as QRegExpValidator
from PySide6.QtWidgets import QApplication
from ui_main_window_6 import Ui_MainWindow
else:
from PyQt5.QtCore import QRegExp, pyqtSignal
from PyQt5.QtGui import QRegExpValidator
from PyQt5.QtWidgets import QApplication
from ui_main_window import Ui_MainWindow
ui2py.py:
import os
import subprocess
# pyuic5 的路径
PYUIC5_PATH = r'D:\Python39\Scripts\pyuic5.exe'
PySide6_PATH = r'D:\Python39\Scripts\pyside6-uic.exe'
# 遍历的根目录路径
ROOT_DIR = 'xxxxxxxx'
def convert_ui_to_py(ui_file_path, py_file_path):
try:
# 使用 subprocess 调用 pyuic5
subprocess.run([PYUIC5_PATH, ui_file_path, "-o", f'{py_file_path}.py'], check = True)
subprocess.run([PySide6_PATH, ui_file_path, "-o", f'{py_file_path}_6.py'], check = True)
print(f"【成功转换】 {ui_file_path}")
except subprocess.CalledProcessError as e:
print(f"【转换 {ui_file_path} 失败: {e}】")
def traverse_dir(root_dir):
"""遍历目录及其子目录,转换 .ui 文件为 .py 文件"""
for dirpath, dirnames, filenames in os.walk(root_dir):
for filename in filenames:
if filename.endswith('.ui'):
ui_file_path = os.path.join(dirpath, filename)
py_file_path = os.path.join(dirpath, filename[:-3]) # 移除 .ui 后缀并添加 .py
convert_ui_to_py(ui_file_path, py_file_path)
def runMain():
print("开始转换...")
traverse_dir(ROOT_DIR)
print("转换完成!")
其他不兼容的地方:
# pyside的字体不支持setWeight
font.setWeight(65)
if pyside_check.HAVE_PYSIDE6:
sys.exit(app.exec())
else:
sys.exit(app.exec_())
# pyside没有desktop()
screen = QApplication.desktop().screenGeometry()
#改为
if pyside_check.HAVE_PYSIDE6:
app = QApplication.instance()
screen = app.primaryScreen()
screen_geometry = screen.geometry()
self.w = screen_geometry.width()
self.h = screen_geometry.height()
else:
screen = QApplication.desktop().screenGeometry()
self.w = screen.width()
self.h = screen.height()