pyside2出现qt.qpa.plugin: Could not find the Qt platform plugin "windows" in ""错误解决办法

系统平台:Win10 64bit
python版本: python 3.5.3
pyside2版本:pyside2-5.11.0(使用pip install -U pyside2安装)

pyside2的API兼容PyQt5,但是它的license是LGPL,便于后面进行商业开发,因此准备玩一下。

在下面地址下载pyside2的示例代码

https://github.com/pyside/pyside2-examples

进入tutorial 目录运行最简单的程序

import sys

from PySide2 import QtWidgets


app = QtWidgets.QApplication(sys.argv)

hello = QtWidgets.QPushButton("Hello world!")
hello.resize(100, 30)

hello.show()

sys.exit(app.exec_())

进入文件路径,打开终端执行

python t1.py

出现了以下错误:

qt.qpa.plugin: Could not find the Qt platform plugin "windows" in ""
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

意思就是找不到Qt平台的插件

网上找了一番解决办法,解决参考链接:https://github.com/pyqt/python-qt5/issues/2
只要将PyQt5改为PySide2就可以了,前面的代码就变为:

import sys
import PySide2

dirname = os.path.dirname(PySide2.__file__)
plugin_path = os.path.join(dirname, 'plugins', 'platforms')
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = plugin_path

from PySide2 import QtWidgets


app = QtWidgets.QApplication(sys.argv)

hello = QtWidgets.QPushButton("Hello world!")
hello.resize(100, 30)

hello.show()

sys.exit(app.exec_())

如果不想每次在代码前添加这段代码,可以修改文件"C:\Applications\WinPython-64bit-3.6.3.0Qt5\python-3.6.3.amd64\Lib\site-packages\PySide2\__init__.py" ,作者用的是WinPython ,使用原生python或者其他集成IDE也一样的,找到对应位置下的pyside2初始化文件,在文件中添加下面的代码即可
这里写图片描述

    dirname = os.path.dirname(__file__)
    plugin_path = os.path.join(dirname, 'plugins', 'platforms')
    os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = plugin_path

到此问题就解决了,再从运行就不会出错了

猜你喜欢

转载自blog.csdn.net/ouening/article/details/81093697
今日推荐