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

相关推荐
吕源林5 小时前
如何解决SQL存储过程连接泄露_确保在异常后关闭连接
jvm·数据库·python
weixin_447443256 小时前
AI启蒙Lean4
python·c#
Ulyanov6 小时前
雷达电子战仿真通信需求与Python实现挑战
python·数据通信·系统仿真·雷达电子对抗仿真
断眉的派大星6 小时前
工厂模式(Factory Pattern)完整详解
python·设计模式
好家伙VCC6 小时前
**基于RISC-V架构的嵌入式系统开发:从零开始构建高效低功耗应用**在当前物联网(IoT)和边缘计
java·python·物联网·架构·risc-v
大佬王6 小时前
WebSocket 连接池生产级实现:实时行情高可用与负载均衡
python·架构
ronindong7 小时前
Cursor 插件分享 | md-couture:一键将 Markdown 转换成带精美样式的 HTML
人工智能·python·ai编程
智慧地球(AI·Earth)7 小时前
规则引擎实战:Python中re库和pyknow库规则引擎实战教程
开发语言·python·程序人生
是小蟹呀^7 小时前
【总结】LangChain中的中间件Middleware
python·中间件·langchain·agent
qq_342295827 小时前
如何为容器内多个列表实现统一滚动条.txt
jvm·数据库·python