https://www.qt.io/zh-cn/qt-for-python
Qt for Python Quick start
https://doc.qt.io/qtforpython/quickstart.html
1.环境需求
python3.6+
虚拟环境(二选一即可):
2.创建虚拟环境
C:\Users\bing.yao>python -m venv quickstart20210602
3.进入虚拟环境
C:\Users\bing.yao>quickstart20210602\Scripts\activate.bat
(quickstart20210602) C:\Users\bing.yao>
4.安装最新版 Qt for Python
(quickstart20210602) C:\Users\bing.yao>pip install pyside6
提示:
Successfully installed pyside6-6.1.0 shiboken6-6.1.0
为何要同时安装Shiboken?
Shiboken有两个用途:
- Generator 生成器:从C或c++头文件中提取信息,并生成CPython代码,允许将C或c++项目带入Python。这个过程使用一个名为ApiExtractor的库,ApiExtractor内部使用Clang。
- Module 模块:一个实用Python模块,公开了新的Python类型、处理指针的函数等,它是用CPython编写的,可以独立于生成器使用。
5.测试安装是否成功
(quickstart20210602) C:\Users\bing.yao>python
Python 3.8.6 (tags/v3.8.6:db45529, Sep 23 2020, 15:52:53) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import PySide6.QtCore
>>> print(PySide6.__version__)
6.1.0
>>> print(PySide6.QtCore.__version__)
6.1.0
6.Hello World
创建 hello_world.py 文件,代码如下:
如果使用记事本创建,记得采用UTF8格式保存。
import sys
import random
from PySide6 import QtCore, QtWidgets, QtGui
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))
if __name__ == "__main__":
app = QtWidgets.QApplication([])
widget = MyWidget()
widget.resize(800, 600)
widget.show()
sys.exit(app.exec())
运行程序
(quickstart20210602) C:\Users\bing.yao>python quickstart20210602\hello_world.py
程序界面如下,点击“Click me!”按钮,切换显示不同语言的 Hello World
![](https://img.haomeiwen.com/i24863986/f39784bf9d351497.png)
网友评论