Qt for python 快速入门

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_())

参考文献

qt 官网

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

高建伟-joe

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值