Qt for Python Quick start
Requirements
- Python 3.6+
- We recommend using a virtual environment, such as venv or virtualenv
Installation
1. Creating and activating an environment
python -m venv env
source env/bin/activate #for Linux and macOS
env\Scripts\activate.bat #for Windows
2. Installation
# Now you are ready to install the Qt for Python packages using pip
# pip 更新到最新版
python -m pip install --upgrade pip
# pip 更新失败 报错“No module named 'pip'”
python -m ensurepip
# 添加国内镜像 加速下载
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
pip install pyside6 #for the latest version.
pip install pyside6==6.0 #for the version 6.0 specifically.
#It is also possible to install a specific snapshot from our servers. To do so, you can use the following command:
pip install --index-url=http://download.qt.io/snapshots/ci/pyside/6.0.0/latest pyside6 --trusted-host download.qt.io
3. Test your installation
# Now that you have Qt for Python installed, test your setup by running the following Python constructs to print version information:
import PySide6.QtCore
# Prints PySide6 version
print(PySide6.__version__)
# Prints the Qt version used to compile PySide6
print(PySide6.QtCore.__version__)
4.Create a Simple Application
# import
import sys
import random
from PySide6 import QtCore, QtWidgets, QtGui
#Main Class
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", alignment=QtCore.Qt.AlignCenter)
self.layout = QtWidgets.QVBoxLayout(self)
self.layout.addWidget(self.text)
self.layout.addWidget(self.button)
self.button.clicked.connect(self.magic)
@QtCore.Slot()
def magic(self):
self.text.setText(random.choice(self.hello))
#Application execution
if __name__ == "__main__":
app = QtWidgets.QApplication([])
widget = MyWidget()
widget.resize(800, 600)
widget.show()
sys.exit(app.exec_())