Python之Qt输出UI

安装PySide2

输入pip install PySide2安装Qt for Python,如果安装过慢需要翻墙,则可以使用国内清华镜像下载,输入命令pip install --user -i https://pypi.tuna.tsinghua.edu.cn/simple PySide2,如下图,

示例Demo

python 复制代码
import random
import sys

from PySide2 import QtCore, QtWidgets, QtGui


class MyWidget(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()

        self.hello = ["您已点击1次", "您已点击2次", "您已点击3次", "您已点击4次"]

        self.button = QtWidgets.QPushButton("请点击按钮")
        self.text = QtWidgets.QLabel("测试用户界面")
        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(600, 400)
    widget.show()
    sys.exit(app.exec_())

可能报错如下,

json 复制代码
Class QMacAutoReleasePoolTracker is implemented in both xxx and xxx. One of the two will be used. Which one is undefined.

该原因是mac版本下安装的opencv包包含一些qt的头文件与PyQt5冲突了,导致无法正确导入相应的包。

输入命令pip uninstall xxx卸载掉冲突的包即可。最后输出UI界面。

相关推荐
_一路向北_1 小时前
爬虫框架:Feapder使用心得
爬虫·python
2022.11.7始学前端1 小时前
n8n第七节 只提醒重要的待办
前端·javascript·ui·n8n
皇族崛起1 小时前
【3D标注】- Unreal Engine 5.7 与 Python 交互基础
python·3d·ue5
世转神风-1 小时前
winDbg专有名词解释
qt·windbg
你想知道什么?2 小时前
Python基础篇(上) 学习笔记
笔记·python·学习
Swizard2 小时前
速度与激情:Android Python + CameraX 零拷贝实时推理指南
android·python·ai·移动开发
尤老师FPGA2 小时前
使用ZYNQ芯片和LVGL框架实现用户高刷新UI设计系列教程(第四十讲)
ui
一直跑2 小时前
Liunx服务器centos7离线升级内核(Liunx服务器centos7.9离线/升级系统内核)
python
leocoder3 小时前
大模型基础概念入门 + 代码实战(实现一个多轮会话机器人)
前端·人工智能·python
Buxxxxxx3 小时前
DAY 37 深入理解SHAP图
python