PyQt PySide2 qt.qpa.plugin: Could not find the Qt platform plugin "windows"

系統(tǒng)平臺:Win10 64bit
python版本: python 3.5.3
pyside2版本:pyside2-5.11.0(使用pip install -U pyside2安裝)

pyside2的API兼容PyQt5,但是它的license是LGPL,便于后面進(jìn)行商業(yè)開發(fā),因此準(zhǔn)備玩一下。

在下面地址下載pyside2的示例代碼

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

進(jìn)入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_())

進(jìn)入文件路徑,打開終端執(zhí)行

python t1.py

出現(xiàn)了以下錯誤:

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平臺的插件

網(wǎng)上找了一番解決辦法,解決參考鏈接:https://github.com/pyqt/python-qt5/issues/2
只要將PyQt5改為PySide2就可以了,前面的代碼就變?yōu)椋?/p>

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也一樣的,找到對應(yīng)位置下的pyside2初始化文件,在文件中添加下面的代碼即可

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

到此問題就解決了,再從運行就不會出錯了

作者:落葉_小唱
來源:CSDN
原文:https://blog.csdn.net/ouening/article/details/81093697?utm_source=copy
版權(quán)聲明:本文為博主原創(chuàng)文章,轉(zhuǎn)載請附上博文鏈接!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容