安装PySide2
输入pip install PySide2
安装Qt for Python,如果安装过慢需要翻墙,则可以使用国内清华镜像下载,输入命令pip install --user -i https://pypi.tuna.tsinghua.edu.cn/simple PySide2
,如下图,
示例Demo
import random
import sys
from PySide2 import QtCore, QtWidgets, QtGui
class MyWidget(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.hello = ["zhangsan", "lisi", "wangermazi", "tom"]
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_())
可能报错如下,
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界面。
补充
如果python解释器部分功能不全,可以输入命令conda update conda
更新anaconda
更新效果如下,
如果仍然报错You might be loading two sets of Qt binaries into the same process. Check that all plugins are compiled against the right Qt binaries. Export DYLD_PRINT_LIBRARIES=1 and check that only one set of binaries are being loaded.
则可通过opencv降级处理。
输入命令pip install --user -i https://pypi.tuna.tsinghua.edu.cn/simple opencv-python==4.8.0.76
安装opencv
。
文章来源:https://www.toymoban.com/news/detail-671633.html
最后安装pyinstaller
后,输入命令pyinstaller --noconsole --onefile --windowed
可以生成不开窗口的可执行文件。文章来源地址https://www.toymoban.com/news/detail-671633.html
到了这里,关于Python之Qt输出UI的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!