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

相关推荐
先知后行。9 分钟前
python的类
开发语言·python
dyxal19 分钟前
Python包导入终极指南:子文件如何成功调用父目录模块
开发语言·python
nnerddboy21 分钟前
解决传统特征波段选择的不可解释性:2. SHAP和LIME
python·机器学习
电商API&Tina22 分钟前
【电商API接口】关于电商数据采集相关行业
java·python·oracle·django·sqlite·json·php
weixin_4215850140 分钟前
解释代码:val_pred = vxm_model.predict(val_input)--与tensor对比
python
xwill*1 小时前
python 字符串拼接
linux·windows·python
superman超哥1 小时前
仓颉锁竞争优化深度解析
c语言·开发语言·c++·python·仓颉
一晌小贪欢1 小时前
【Python办公自动化】Python办公自动化常用库新手指南
开发语言·python·python自动化办公·python3·python办公自动化·python办公
其美杰布-富贵-李1 小时前
Python 反射完整学习笔记
笔记·python·学习·反射
热心市民小刘05051 小时前
12.23二叉树的层序遍历
python