2017-11-18 159 views
1

我正在使用Python 3.6和PyQt-5.9.1與Qt Designer進行UI設計,我需要包含一些matplotlib圖。我發現爲了不修改從ui文件生成的python代碼,每次更新gui部分(這是Qt Designer的預期),我需要一個MatplotlibWidget插件set-在Qt設計器中。Qt5 Matplotlib Designer插件

我的問題是,我發現這個插件的引用是來自基於Python 2.7的Python(x,y)包,並且不適用於Python 3.x;或者PyQtdesginerplugins包在Python 3.x/Qt5環境中不起作用(已在此問題How do I add matplotlib plugin to Qt5.4 Designer plugins?中指出)。

有沒有可能在我的Python 3.6和Qt5環境中添加一個MatplotlibWidget到Qt Designer?這將有助於我們充分利用Qt Designer從應用程序邏輯中分離出ui。

+0

我不知道是否有這樣的插件可用。但通常你不會需要它。您只需在GUI中留下一些空間,並通過代碼將matplotlib畫布(它是一個QWidget)添加到它。 – ImportanceOfBeingErnest

+0

如果你看,例如在[這個問題](https://stackoverflow.com/questions/46391356/embed-a-matplotlib-graphic-into-a-widget-pyqt5)有一部分是通過一些用戶界面設計師和部分是運行應用程序的代碼。你不會操縱前者,但在後者中添加你的小部件。 – ImportanceOfBeingErnest

+0

感謝promt回覆。事實上,它只需少量額外的工作就可以這樣工作除非別人知道一個神奇的qtdesigner插件,否則我會走這條路。 – Thib

回答

1

下面你會發現一個用於Qt設計器的MatplotlibPlugin的PyQt5版本。

要使用它,請將兩個文件放在PYQTDESIGNERPATH環境變量中包含的目錄中,或放入directories that Qt Designer searches for its own plugins之一的「python」子目錄中。這兩個文件必須完全按照如下所示進行命名。

matplotlibwidget.py

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

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' 
+0

謝謝!我嘗試了你所說的,但我無法讓Matplotlib小部件顯示在Qtdesigner界面中。 – Thib

+0

你是如何設置'PYQTDESIGNERPATH'的?你在哪個平臺上? – ekhumoro

+0

謝謝!我嘗試了你所說的,但我無法讓Matplotlib小部件顯示在Qtdesigner界面中。我已將.py文件複製到設計器插件目錄中,並添加了envt變量。看起來行Qtdesigner預計.dll作爲插件,至少當我進入插件信息框,我只看到pyqt5.dll – Thib