1. 程式人生 > >Qt for Python 5.12初體驗

Qt for Python 5.12初體驗

Qt for Python 5.12初體驗

2018年12月18日,Qt在其部落格上宣佈Qt for Python 5.12正式釋出,按照其幫助文件的說明,嘗試運行了第一個小例子。
首先需要安裝Python 3.5+ or 2.7和for Qt 5.12,安裝完成之後邊可以輸入程式碼編譯運行了。
複製自Qt示例的原始碼如下:

1 import sys
2 from PySide2.QtWidgets import QApplication, QLabel
3 
4 app = QApplication(sys.argv)
5 label = QLabel("Hello World!")
6
label.show() 7 app.exec_()

編譯執行,提示錯誤如下:
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.
找不到外掛的路徑,這裡是解決方案。修改之後的程式碼如下:

 1 import os
 2 import
sys 3 import PySide2 4 from PySide2.QtWidgets import QApplication, QLabel 5 6 dirname = os.path.dirname(PySide2.__file__) 7 plugin_path = os.path.join(dirname, 'plugins', 'platforms') 8 os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = plugin_path 9 10 app = QApplication(sys.argv) 11 label = QLabel("
Hello World") 12 label.show() 13 sys.exit(app.exec_())

成功執行,並彈出對話方塊。