可以在Virtualenv(Ubuntu)中播放QT设计器中获取自定义Pyqt5小部件插件
我正在尝试使用PYQT设计器上的自定义小部件,使用: https://github.com/baoboa/pyqt5/blob/master/examples/designer/designer/plugins/plugins/plugins.py 以及插件和窗口 https://github.com/tranter/blogs/blogs/blogs/tree/master/master/pythonplugin/pythonplugin 。
我的设计师通过apt-get Designer
和按照
QlibraryInfo.location(QlibraryInfo.BinariesPath)
在/usr/usr/usr/lib/qt5/bin
LED小部件和插件在运行脚本的同一级别中位于两个目录(请参阅代码):
#!/usr/bin/env python3
#############################################################################
##
## Copyright (C) 2013 Riverbank Computing Limited.
## All rights reserved.
##
## This file is part of the examples of PyQt.
##
## You may use this file under the terms of the BSD license as follows:
##
## "Redistribution and use in source and binary forms, with or without
## modification, are permitted provided that the following conditions are
## met:
## * Redistributions of source code must retain the above copyright
## notice, this list of conditions and the following disclaimer.
## * Redistributions in binary form must reproduce the above copyright
## notice, this list of conditions and the following disclaimer in
## the documentation and/or other materials provided with the
## distribution.
## * Neither the name of Riverbank Computing Limited nor the names of
## its contributors may be used to endorse or promote products
## derived from this software without specific prior written
## permission.
##
## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
##
#############################################################################
import sys
import os
from PyQt5.QtCore import QLibraryInfo, QProcess, QProcessEnvironment
from PyQt5.QtWidgets import QApplication, QMessageBox
designer_bin = QLibraryInfo.location(QLibraryInfo.BinariesPath)
print('designer_bin = QLibraryInfo.location(QLibraryInfo.BinariesPath) :', designer_bin)
app = QApplication(sys.argv)
QMessageBox.information(None, "PyQt Designer Plugins",
"<p>This example will start Qt Designer when you click the <b>OK</b> "
"button.</p>"
"<p>Before doing so it sets the <tt>PYQTDESIGNERPATH</tt> environment "
"variable to the <tt>python</tt> directory that is part of this "
"example. This directory contains all the example Python plugin "
"modules.</p>"
"<p>It also sets the <tt>PYTHONPATH</tt> environment variable to the "
"<tt>widgets</tt> directory that is also part of this example. This "
"directory contains the Python modules that implement the example "
"custom widgets.</p>"
"<p>All of the example custom widgets should then appear in "
"Designer's widget box in the <b>PyQt Examples</b> group.</p>")
# Tell Qt Designer where it can find the directory containing the plugins and
# Python where it can find the widgets.
base = os.path.dirname(__file__)
print('base :',base)
env = QProcessEnvironment.systemEnvironment()
env.insert('PYQTDESIGNERPATH', os.path.join(base, 'python3'))
env.insert('PYTHONPATH', os.path.join(base, 'widgets3'))
# Start Designer.
designer = QProcess()
designer.setProcessEnvironment(env)
designer_bin = '/usr/lib/qt5/bin/designer'
print('\ndesigner_bin :', designer_bin)
def process_finished():
#designer.waitForFinished(-1)
print('finished')
sys.exit()
designer.start(designer_bin)
#designer.setProgram(designer_bin) #anche con questo non cambia da virtualenv non funza
#designer.startDetached() # da usare insieme a sopra
designer.finished.connect(process_finished)
print('designer.pid() :', designer.pid())
print(env.keys())
print('\nenv PYTHONPATH :', env.value('PYTHONPATH'))
print('\nenv PYQTDESIGNERPATH :', env.value('PYQTDESIGNERPATH'))
sys.exit(app.exec_())
当我在Main Enviroment中启动脚本时,我可以看到我的自定义窗口:
从Virtualenv内部运行脚本,LED自定义窗口小部件不会显示。
我可以通过pip安装pyqt5-tools
和运行
pyqt5-tools设计器-p ** path_to_to_both_widget.py_widgetplugin.py **
让我的自定义
窗口>可用,但我不知道为什么第一种方法不起作用,也让我发疯,对此有任何想法
I am trying to use custom widget on PyQt Designer, using code from: https://github.com/baoboa/pyqt5/blob/master/examples/designer/plugins/plugins.py and both plugin and widget from
https://github.com/tranter/blogs/tree/master/PythonPlugin.
My Designer is installed win ubuntu via apt-get designer
and as per
QLibraryInfo.location(QLibraryInfo.BinariesPath)
is in /usr/lib/qt5/bin
the led widget and plugin are in two directories at same level of the run script (see the code):
#!/usr/bin/env python3
#############################################################################
##
## Copyright (C) 2013 Riverbank Computing Limited.
## All rights reserved.
##
## This file is part of the examples of PyQt.
##
## You may use this file under the terms of the BSD license as follows:
##
## "Redistribution and use in source and binary forms, with or without
## modification, are permitted provided that the following conditions are
## met:
## * Redistributions of source code must retain the above copyright
## notice, this list of conditions and the following disclaimer.
## * Redistributions in binary form must reproduce the above copyright
## notice, this list of conditions and the following disclaimer in
## the documentation and/or other materials provided with the
## distribution.
## * Neither the name of Riverbank Computing Limited nor the names of
## its contributors may be used to endorse or promote products
## derived from this software without specific prior written
## permission.
##
## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
##
#############################################################################
import sys
import os
from PyQt5.QtCore import QLibraryInfo, QProcess, QProcessEnvironment
from PyQt5.QtWidgets import QApplication, QMessageBox
designer_bin = QLibraryInfo.location(QLibraryInfo.BinariesPath)
print('designer_bin = QLibraryInfo.location(QLibraryInfo.BinariesPath) :', designer_bin)
app = QApplication(sys.argv)
QMessageBox.information(None, "PyQt Designer Plugins",
"<p>This example will start Qt Designer when you click the <b>OK</b> "
"button.</p>"
"<p>Before doing so it sets the <tt>PYQTDESIGNERPATH</tt> environment "
"variable to the <tt>python</tt> directory that is part of this "
"example. This directory contains all the example Python plugin "
"modules.</p>"
"<p>It also sets the <tt>PYTHONPATH</tt> environment variable to the "
"<tt>widgets</tt> directory that is also part of this example. This "
"directory contains the Python modules that implement the example "
"custom widgets.</p>"
"<p>All of the example custom widgets should then appear in "
"Designer's widget box in the <b>PyQt Examples</b> group.</p>")
# Tell Qt Designer where it can find the directory containing the plugins and
# Python where it can find the widgets.
base = os.path.dirname(__file__)
print('base :',base)
env = QProcessEnvironment.systemEnvironment()
env.insert('PYQTDESIGNERPATH', os.path.join(base, 'python3'))
env.insert('PYTHONPATH', os.path.join(base, 'widgets3'))
# Start Designer.
designer = QProcess()
designer.setProcessEnvironment(env)
designer_bin = '/usr/lib/qt5/bin/designer'
print('\ndesigner_bin :', designer_bin)
def process_finished():
#designer.waitForFinished(-1)
print('finished')
sys.exit()
designer.start(designer_bin)
#designer.setProgram(designer_bin) #anche con questo non cambia da virtualenv non funza
#designer.startDetached() # da usare insieme a sopra
designer.finished.connect(process_finished)
print('designer.pid() :', designer.pid())
print(env.keys())
print('\nenv PYTHONPATH :', env.value('PYTHONPATH'))
print('\nenv PYQTDESIGNERPATH :', env.value('PYQTDESIGNERPATH'))
sys.exit(app.exec_())
When I launch the script in main enviroment I can see my custom widget:
but when I run the script from inside a virtualenv the led custom widget doesnt show up.
I can install designer in my virtualenv via pip install pyqt5-tools
and the run
pyqt5-tools designer -p **PATH_to_both_widget.py_widgetplugin.py**
to have my custom
widgets available, but I can't figure out why the first approach doesnt work and is draving me crazy, any idea about it
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
关键是要修改
include
方法,因为导入是从那里构建的,对于我的项目结构:designer.py
找到完整的示例在这里。
The key is to modify the
includeFile
method since the import is built from there, for my project structure:designer.py
The complete example is found here.