pypSide2

pypSide2

PySide2是QT官方出的Python的QT封装
1. pip安装
pip3/pip install PySide2
如果安装太慢,可以尝试:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyside2
通过http下载安装 注意版本号根据自己的需要选择相应的版本
pip install –index-url=http://download.qt.io/snapshots/ci/pyside/5.13.0/latest pyside2 –trusted-host download.qt.io

  1. conda安装
    添加清华源,注意是 http:
    conda config –add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
    conda config –add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
    conda config –add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
    conda config –set show_channel_urls yes

如果没有解决的话,在C:\Users\用户名 目录下修改 Anaconda 配置文件 .condarc,将 -defaults 这一行删掉即可

ssl_verify: true
channels:
  - http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
  - http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
  - http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
show_channel_urls: true

conda install PySide2

Designer位于 /Users/sweet/opt/anaconda3/bin
打开使用 open -a Designer
转换 pyside2-uic d1.ui > ui_mainwindow.py
pyside2-uic d1.ui -o d1_ui.py
pyuic5 -o d1.ui d1_ui.py

import sys
from PySide2.QtWidgets import QApplication, QMainWindow
from PySide2.QtCore import QFile
from ui_mainwindow import Ui_MainWindow

class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

if __name__ == "__main__":
    app = QApplication(sys.argv)

    window = MainWindow()
    window.show()

    sys.exit(app.exec_())
或者直接使用原UI文件
import sys
from PySide2.QtUiTools import QUiLoader
from PySide2.QtWidgets import QApplication
from PySide2.QtCore import QFile

if __name__ == "__main__":
    app = QApplication(sys.argv)

    ui_file = QFile("mainwindow.ui")
    ui_file.open(QFile.ReadOnly)

    loader = QUiLoader()
    window = loader.load(ui_file)
    ui_file.close()
    window.show()

    sys.exit(app.exec_())

支持的模块资料
pyside2 https://doc.qt.io/qtforpython/modules.html
qt5 https://doc.qt.io/qt-5/qtmodules.html

建立.pro文件
translation_en_to_zh_CN.pro
SOURCES = ui_mainwindow.py
TRANSLATIONS = translation_en_to_zh_CN.ts
CODECFORTR = UTF-8
CODECFORSRC = UTF-8

生成语言转换文件.ts
pyside2-lupdate translation_en_to_zh_CN.pro

使用 qt linguist 打开 .ts 文件来翻译成各语言版本
TRANSLATOR = QtCore.QTranslator()
app.installTranslator(TRANSLATOR) # 非常重要的一步,为 app 安装 TRANSLATOR,如果不安装,是没有效果的

pyinstaller -F main.py
-F表示生成一个独立的可执行的exe文件。-m表示生成的文件不带cmd
python pyinstaller.py -F -w test.py
这里-w是不生成console窗口,仅显示UI。
pip install pywin32
pip install pyinstaller


问题故障

  1. qt.qpa.plugin: Could not load the Qt platform plugin “windows” in “” even though it was found.
    This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.
    Available platform plugins are: direct2d, minimal, offscreen, windows.
    解决:
    import os
    envpath = r’D:\anaconda3\Lib\site-packages\PySide2\plugins\platforms’
    os.environ[“QT_QPA_PLATFORM_PLUGIN_PATH”] = envpath

  2. You might be loading two sets of Qt binaries into the same process. Check that all plugins are compiled against the right Qt binaries. Export DYLD_PRINT_LIBRARIES=1 and check that only one set of binaries are being loaded.
    qt.qpa.plugin: Could not load the Qt platform plugin “cocoa” in “” even though it was found.
    This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.
    解决:多个版本,保留一个

You must be logged in to post a comment