PySide2的在线安装与入门实例

PySide2是为 Python 语言提供 Qt 集成的模块。
安装命令:pip install pyside2

地址在这里: 
https://fredrikaverpil.github.io/2018/03/09/official-pyside2-wheels/

命令是:

pip install --index-url=http://download.qt.io/snapshots/ci/pyside/5.11/latest/ pyside2 --trusted-host download.qt.io

官方文档地址:

https://pypi.org/project/PySide2/

https://doc-snapshots.qt.io/qtforpython/index.html

其他相关资源: 
https://fredrikaverpil.github.io/blog/tag/pyside/

1. 安装

Python环境:3.7.3

  1. 从PyPi下载安装:
    pip install PySide2 # For the latest version on PyPi
  2. 通过http下载安装:
    pip install --index-url=http://download.qt.io/snapshots/ci/pyside/5.12/latest pyside2 --trusted-host download.qt.io

2. 测试是否安装成功

import PySide2.QtCore

# Prints PySide2 version
print(PySide2.__version__)
# 5.12.2

# Gets a tuple with each version component
print(PySide2.__version_info__)
# (5, 12, 2, '', '')

# Prints the Qt version used to compile PySide2
print(PySide2.QtCore.__version__)
# 5.12.2

# Gets a tuple with each version components of Qt used to compile PySide2
print(PySide2.QtCore.__version_info__)
# (5, 12, 2)

# Print the current running Qt version number
print(PySide2.QtCore.qVersion())
# 5.12.2

3. Hello world!

import sys
import random
from PySide2 import QtCore, QtWidgets, QtGui

# Define a class named MyWidget, 
# which extends QWidget and includes a QPushButton and QLabel
class MyWidget(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()

        self.hello = ["你好,世界", "Hallo Welt", "Hei maailma", "Hola Mundo", "Привет мир"]

        self.button = QtWidgets.QPushButton("Click me!")
        self.text = QtWidgets.QLabel("Hello World")
        self.text.setAlignment(QtCore.Qt.AlignCenter)

        self.layout = QtWidgets.QVBoxLayout()
        self.layout.addWidget(self.text)
        self.layout.addWidget(self.button)
        self.setLayout(self.layout)

        self.button.clicked.connect(self.magic)

    def magic(self):
        self.text.setText(random.choice(self.hello))


if __name__ == "__main__":
    app = QtWidgets.QApplication([])

    widget = MyWidget()
    widget.resize(800, 600)
    widget.show()

准备安装
在安装Qt for Python之前,必须安装以下软件:
Python 3.5+或2.7
libclang 5.0+(适用于Qt 5.11)或6.0+(适用于Qt 5.12)
推荐:虚拟环境,例如venv或virtualenv
为Python安装Qt
安装完所需的软件后,即可使用pip wheel安装Qt for Python软件包。从命令提示符运行以下命令以安装

pip install PySide2 # For the latest version on PyPi

说实话这样太慢了,所以我从QT官网直接安装。

pip install --index-url=http://download.qt.io/snapshots/ci/pyside/5.12/latest pyside2 --trusted-host download.qt.io

以上是官方给的推荐,而我用的QT是5.13.0的所以修改一下上面的目录就行了

pip install --index-url=http://download.qt.io/snapshots/ci/pyside/5.13.0/latest pyside2 --trusted-host download.qt.io

发表评论

电子邮件地址不会被公开。 必填项已用*标注