我想在我的PyQt4 GUI中创建一个导航工具栏,但是我不能让它工作,我必须承认我大部分是复制粘贴的代码,我并没有真正得到它。如果你能告诉我如何添加导航工具栏并解释它是如何生成的,那就太好了。提前谢谢你。
顺便说一句,我见过This Post但我相信它和我的不同,我不知道如何使用它。
from PyQt4 import QtGui
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
class MplCanvas(FigureCanvas):
def __init__( self ):
self.fig = Figure()
self.ax = self.fig.add_subplot( 111 )
FigureCanvas.__init__( self, self.fig )
FigureCanvas.setSizePolicy( self, QtGui.QSizePolicy.Expanding,QtGui.QSizePolicy.Expanding )
FigureCanvas.updateGeometry( self )
class matplotlibWidget(QtGui.QWidget):
def __init__( self, parent = None ):
QtGui.QWidget.__init__( self, parent )
self.canvas = MplCanvas()
#self.toolbar = self.canvas.toolbar #Dunno How :(
self.vbl = QtGui.QVBoxLayout()
self.vbl.addWidget( self.canvas )
self.setLayout( self.vbl )
最佳答案
我设法解决了这个问题,代码应该是这样的
from PyQt4 import QtGui
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4 import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
class MplCanvas(FigureCanvas):
def __init__( self ):
self.fig = Figure()
self.ax = self.fig.add_subplot( 111 )
FigureCanvas.__init__( self, self.fig )
FigureCanvas.setSizePolicy( self, QtGui.QSizePolicy.Expanding,QtGui.QSizePolicy.Expanding )
FigureCanvas.updateGeometry( self )
class matplotlibWidget(QtGui.QWidget):
def __init__( self, parent = None ):
QtGui.QWidget.__init__( self, parent )
self.canvas = MplCanvas() #create canvas that will hold our plot
self.navi_toolbar = NavigationToolbar(self.canvas, self) #createa navigation toolbar for our plot canvas
self.vbl = QtGui.QVBoxLayout()
self.vbl.addWidget( self.canvas )
self.vbl.addWidget(self.navi_toolbar)
self.setLayout( self.vbl )