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

相关推荐
晚秋大魔王2 小时前
基于python的jlink单片机自动化批量烧录工具
前端·python·单片机
胖哥真不错2 小时前
Python基于PyTorch实现多输入多输出进行CNN卷积神经网络回归预测项目实战
pytorch·python·毕业设计·课程设计·毕设·多输入多输出·cnn卷积神经网络回归预测
程序员-小李2 小时前
基于PyTorch的动物识别模型训练与应用实战
人工智能·pytorch·python
闲人编程6 小时前
Python在网络安全中的应用:编写一个简单的端口扫描器
网络·python·web安全·硬件·端口·codecapsule·扫描器
Mr_Xuhhh9 小时前
GUI自动化测试--自动化测试的意义和应用场景
python·集成测试
2301_764441339 小时前
水星热演化核幔耦合数值模拟
python·算法·数学建模
循环过三天9 小时前
3.4、Python-集合
开发语言·笔记·python·学习·算法
Q_Q5110082859 小时前
python+django/flask的眼科患者随访管理系统 AI智能模型
spring boot·python·django·flask·node.js·php
SunnyDays101111 小时前
如何使用Python高效转换Excel到HTML
python·excel转html
Q_Q51100828511 小时前
python+django/flask的在线学习系统的设计与实现 积分兑换礼物
spring boot·python·django·flask·node.js·php