系統(tǒng)平臺(tái):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 目錄運(yùn)行最簡單的程序
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)了以下錯(cuò)誤:
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平臺(tái)的插件
網(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也一樣的,找到對(duì)應(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
到此問題就解決了础淤,再從運(yùn)行就不會(huì)出錯(cuò)了
作者:落葉_小唱
來源:CSDN
原文:https://blog.csdn.net/ouening/article/details/81093697?utm_source=copy
版權(quán)聲明:本文為博主原創(chuàng)文章,轉(zhuǎn)載請(qǐng)附上博文鏈接哨苛!