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_())

成功运行,并弹出对话框。

猜你喜欢

转载自www.cnblogs.com/ljy339/p/10185151.html