1. 安装
Python环境:3.7.3
- 从PyPi下载安装:
pip install PySide2 # For the latest version on PyPi
- 通过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()
sys.exit(app.exec_())