简体   繁体   English

将工具栏添加到PyQt4中的MPL图形

[英]Adding a toolbar to a MPL figure in PyQt4

I have a very simple pyqt4 application that embeds a MatPlotLib figure. 我有一个非常简单的pyqt4应用程序,它嵌入了一个MatPlotLib图。 I am embedding the matplotlib figure through the MatplotlibWidget and I created the interface through the QtDesigner along with pyuic4 . 我通过嵌入的MatplotlibWidget的matplotlib图和我创建通过QtDesigner接口连同pyuic4

I would like to provide the user access to the toolbar for interactive navigation . 我想为用户提供访问交互式导航工具栏的权限。 But, despite their nice example on GTK, I can't seem to get it to work for pyQt. 但是,尽管他们在GTK上有很好的例子,但我似乎无法让它适用于pyQt。 It mentions examples, but the example for QT4 provided does not include the toolbar. 它提到了示例,但提供的QT4示例不包括工具栏。

I appreciate any help with this. 我很感激任何帮助。


This question is similar, but does not quite address what I need and I have not been able to adapt it. 这个问题很相似,但并没有完全解决我需要的问题,而且我无法适应它。

There is no toolbar widget in QtDesigner, but you can add the toolbar by code: QtDesigner中没有工具栏小部件,但您可以按代码添加工具栏:

Here is the example, the plot_layout is a QVBoxLayout designed by QtDesigner, and plot_canvas is the MatplotlibWidget widget. 以下是示例, plot_layout是由QVBoxLayout设计的QVBoxLayout, plot_canvasMatplotlibWidget小部件。

import numpy as np
from PyQt4.QtCore import Qt
from PyQt4.QtGui import *
from matplotlib.backends.backend_qt4 import NavigationToolbar2QT as NavigationToolbar
from plot_dialog2 import Ui_Form

class PlotDialog(QWidget, Ui_Form):
    def __init__(self):
        QWidget.__init__(self)
        self.setupUi(self)
        self.navi_toolbar = NavigationToolbar(self.plot_canvas, self)
        self.plot_layout.addWidget(self.navi_toolbar)

if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    dialog = PlotDialog()
    dialog.show()
    sys.exit(app.exec_())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM