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界面。

相关推荐
蓝博AI5 小时前
2027基于yolov8飞机蒙皮缺陷检测,支持图像,视频和摄像实时检测,python源码
开发语言·python·yolo
是菜鸡小小陈啊5 小时前
【CS329Z Assignment 1】从零构建 LLM Agent Harness(学习笔记)08
笔记·python·学习·大模型·llm
人工智能技术咨询.7 小时前
高质量数据集的一致性
python
IT小小猪8 小时前
子进程闪黑窗:pythonw 没有控制台,0x08000000 一个参数解决
运维·windows·python·自动化·进程管理
2601_962885728 小时前
如何用 Python 计算终极振荡器(Ultimate Oscillator, UOS)?
开发语言·python
Thanks_ks9 小时前
Python 列表与元组:可变性与不可变性的差异及使用场景选择
数据结构·python·列表·元组·可变对象·不可变对象·可哈希
benchmark_cc10 小时前
一次获取多只股票实时行情:QuantDash `quotes.get(symbols=[...])` 批量快照
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
geovindu10 小时前
python: PCA
开发语言·后端·python·算法
AI模型调用笔记11 小时前
流式 API 的 usage 为 null?用 cURL 看尾帧,别漏空 choices
python
天诰12 小时前
SA-T-06 轨迹数据工程 脏数据检测
人工智能·python·算法·语言模型·ai编程