让linux下的qt4设计器使用python3处理自定义小部件插件
我使用此脚本启动 qt 设计器,其中自定义小部件(在 Python 3 中)可见:
#!/usr/bin/env python3
import os, sys, subprocess
curDir = os.path.dirname(os.path.abspath(__file__))
params = list(sys.argv) # copy list
params[0] = 'designer' # "designer-qt4" on Linux
widgetsDir = os.path.join(curDir, 'wic', 'widgets')
# add search path for custom widgets and plugins for designer
os.putenv('PYQTDESIGNERPATH', widgetsDir)
subprocess.Popen(params)
但看起来设计器正在使用 python 2.7 来使用小部件插件:
vic@ubuntu:~/wic$ python3 qt_designer.pyw
vic@ubuntu:~/wic$ File "/home/vic/wic/wic/widgets/w_date_edit_plugin.py", line 63
app.exec()
^
SyntaxError: invalid syntax
如何指示设计器使用 Python 3,而不是 Python 2?
我使用 Kubuntu 11.10、KDE 4.7.2、python3.2 和 python2.7,为 Python 3 编译的 PyQt v4.8.5
I use this script to start qt designer with my custom widgets (in Python 3) visible:
#!/usr/bin/env python3
import os, sys, subprocess
curDir = os.path.dirname(os.path.abspath(__file__))
params = list(sys.argv) # copy list
params[0] = 'designer' # "designer-qt4" on Linux
widgetsDir = os.path.join(curDir, 'wic', 'widgets')
# add search path for custom widgets and plugins for designer
os.putenv('PYQTDESIGNERPATH', widgetsDir)
subprocess.Popen(params)
But it looks like designer is using python 2.7 to use the widget plugins:
vic@ubuntu:~/wic$ python3 qt_designer.pyw
vic@ubuntu:~/wic$ File "/home/vic/wic/wic/widgets/w_date_edit_plugin.py", line 63
app.exec()
^
SyntaxError: invalid syntax
How to instruct designer to use Python 3, not Python 2?
I use Kubuntu 11.10, KDE 4.7.2, python3.2 and python2.7, PyQt v4.8.5 compiled for Python 3
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来 PyQt 不允许并行安装处理自定义小部件的设计器插件 (
libpythonplugin.so
)。因此,通常会有一个插件链接到 python2 或 python3,但不会同时链接到两者。看起来 Kubuntu 目前安装了该插件的 python2 版本(在我的 Linux 系统上,情况正好相反)。如果您想要该插件的 python3 版本,只需从源代码编译一个替代品即可。
编辑
要编译替换版本,首先确保您已安装
sip
软件包。我不是 ubuntu 方面的专家,但我认为您将需要 python-sip-dev 和 python3-sip-dev 软件包(当然还有任何依赖项)。接下来,下载与系统上安装的版本匹配的 PyQt4 源。我能够在这里找到一些 ubuntu pyqt 源包 。
现在解压 tarball,cd 到生成的源目录(对于 Kubuntu 11.10 来说应该是
PyQt-x11-gpl-4.8.5
),然后使用 python3 配置构建:如果完成时没有错误,构建它(但不要安装它):
使用上述配置选项,在我的旧设备上编译 pyqt 大约需要 5 分钟i686-AMD64-X2-6000 系统。完成后,
libpythonplugin.so
插件应位于PyQt-x11-gpl-4.8.5/designer
目录中。您现在可以备份并删除现有插件(在我的系统上,它位于
/usr/lib/qt/plugins/designer
目录中),然后复制新插件。It looks like PyQt does not allow for side-by-side installation of the designer plugin that handles custom widgets (
libpythonplugin.so
). So there will normally be a single plugin linked against either python2, or python3, but not both.It would appear that Kubuntu currently installs the python2 version of the plugin (on my linux system, it's the other way around). If you want a python3 version of the plugin, just compile a replacement from source.
EDIT
To compile a replacement, first ensure you have the
sip
packages installed. I am no expert on ubuntu, but I think you will need thepython-sip-dev
andpython3-sip-dev
packages (plus any dependencies, of course).Next, download the PyQt4 sources that match the version installed on your system. I was able to find some ubuntu pyqt source packages here.
Now unpack the tarball, cd into the resulting source directory (looks like it should be
PyQt-x11-gpl-4.8.5
for Kubuntu 11.10), and then configure the build using python3:If that completes without error, build it (but do not install it):
Using the above configuration options it takes about 5 mins to compile pyqt on my old i686-AMD64-X2-6000 system. Once complete, the
libpythonplugin.so
plugin should be in thePyQt-x11-gpl-4.8.5/designer
directory.You can now backup and remove the existing plugin (on my system it's in the
/usr/lib/qt/plugins/designer
directory), and copy over your new plugin.