首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Qt5 Matplotlib设计器插件

问Qt5 Matplotlib设计器插件
EN

Stack Overflow用户
提问于 2017-11-18 18:06:51
回答 2查看 5.2K关注 0票数 2

我正在开发一个应用程序,使用Python 3.6和PyQt-5.9.1与Qt Designer进行UI设计,我需要包括一些matplotlib图。

经过深入研究,我发现为了不在每次更新gui部件时修改从ui文件生成的python代码(这是Qt Designer所期望的),我需要在Qt Designer中设置一个MatplotlibWidget插件。

我的问题是,我找到的这个插件的引用要么来自基于Python2.7的Python(x,y)包,不能在Python3.x上工作;要么来自在Python3.x/QT5环境下也不能工作的PyQtdesginerplugins包(已经在这个问题How do I add matplotlib plugin to Qt5.4 Designer plugins?中指出了)。

在我的Python3.6和Qt5环境中,有没有可能向Qt设计器添加MatplotlibWidget?这将有助于充分利用Qt Designer将ui与应用程序逻辑分离的优势。

EN

回答 2

Stack Overflow用户

发布于 2017-11-18 23:09:10

下面是Qt Designer的MatplotlibPlugin的PyQt5版本。

要使用它,请将这两个文件放在一个包含在PYQTDESIGNERPATH环境变量中的目录中,或者放在其中一个directories that Qt Designer searches for its own plugins中的"python“子目录中。这两个文件的名称必须完全如下所示。

matplotlibwidget.py

代码语言:javascript
复制
from PyQt5.QtCore import QSize
from PyQt5.QtWidgets import QSizePolicy
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as Canvas
from matplotlib.figure import Figure
from matplotlib import rcParams

rcParams['font.size'] = 9


class MatplotlibWidget(Canvas):
    def __init__(self, parent=None, title='', xlabel='', ylabel='',
                 xlim=None, ylim=None, xscale='linear', yscale='linear',
                 width=4, height=3, dpi=100):
        self.figure = Figure(figsize=(width, height), dpi=dpi)
        self.axes = self.figure.add_subplot(111)
        self.axes.set_title(title)
        self.axes.set_xlabel(xlabel)
        self.axes.set_ylabel(ylabel)
        if xscale is not None:
            self.axes.set_xscale(xscale)
        if yscale is not None:
            self.axes.set_yscale(yscale)
        if xlim is not None:
            self.axes.set_xlim(*xlim)
        if ylim is not None:
            self.axes.set_ylim(*ylim)

        super(MatplotlibWidget, self).__init__(self.figure)
        self.setParent(parent)
        super(MatplotlibWidget, self).setSizePolicy(
            QSizePolicy.Expanding, QSizePolicy.Expanding)
        super(MatplotlibWidget, self).updateGeometry()

    def sizeHint(self):
        return QSize(*self.get_width_height())

    def minimumSizeHint(self):
        return QSize(10, 10)

matplotlibplugin.py

代码语言:javascript
复制
import os
from PyQt5.QtGui import QIcon
from PyQt5.QtDesigner import QPyDesignerCustomWidgetPlugin
from matplotlib import rcParams
from matplotlibwidget import MatplotlibWidget

rcParams['font.size'] = 9


class MatplotlibPlugin(QPyDesignerCustomWidgetPlugin):
    def __init__(self, parent=None):
        super(MatplotlibPlugin, self).__init__(parent)
        self._initialized = False

    def initialize(self, editor):
        self._initialized = True

    def isInitialized(self):
        return self._initialized

    def createWidget(self, parent):
        return MatplotlibWidget(parent)

    def name(self):
        return 'MatplotlibWidget'

    def group(self):
        return 'PyQt'

    def icon(self):
        return QIcon(os.path.join(
            rcParams['datapath'], 'images', 'matplotlib.png'))

    def toolTip(self):
        return ''

    def whatsThis(self):
        return ''

    def isContainer(self):
        return False

    def domXml(self):
        return '<widget class="MatplotlibWidget" name="mplwidget">\n' \
               '</widget>\n'

    def includeFile(self):
        return 'matplotlibwidget'
票数 3
EN

Stack Overflow用户

发布于 2019-11-06 07:13:02

@Thib。我正在努力解决同样的问题,但我让它工作了。正如@ekhumoro建议的那样,您可以将python文件放在任何位置,然后将该文件夹添加到环境路径中,但是如果您使用pyqt5-tools安装了QT designer,则需要运行Scripts\pyqt5designer.exe (根据您的设置,它位于python虚拟环境或python安装文件夹中),这是基于上面提供的路径所做的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47364804

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档