我有一个PyQt窗口,需要同时具有绘图和导航工具栏。但是,我似乎无法使两个对象啮合在一起。也就是说,将工具栏放在图的顶部,将相同的水平长度扩展,适当缩放且不重叠。以下是我的最低限度的工作示例。非常感谢您的光临!
import sys, os
from pyface.qt import QtGui, QtCore
os.environ['ETS_TOOLKIT'] = 'qt4'
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
import random
class App(QtGui.QMainWindow):
def __init__(self):
super(App, self).__init__()
self.left = 10
self.top = 10
self.title = 'Minimum, working example'
self.width = 640
self.height = 400
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
m = PlotCanvas(self, width=5, height=4)
m.move(0,0)
self.show()
class PlotCanvas(FigureCanvas):
def __init__(self, parent=None, width=5, height=4, dpi=100):
fig = Figure(figsize=(width, height), dpi=dpi)
self.axes = fig.add_subplot(111)
FigureCanvas.__init__(self, fig)
self.setParent(parent)
FigureCanvas.updateGeometry(self)
##########################
#trouble seems to be here:
fig.set_tight_layout(True)
self.canvas = FigureCanvas(fig)
self.toolbar = NavigationToolbar(self.canvas, self)
##########################
self.plot()
def plot(self):
data = [random.random() for i in range(25)]
ax = self.figure.add_subplot(111)
ax.plot(data, 'r-')
ax.set_title("Is this getting overlapped by the toolbar?")
self.draw()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
答案 0 :(得分:1)
使用self.canvas = FigureCanvas (fig)
时要创建另一个FigureCanvas
,为什么要创建另一个?,则没有必要。
为什么它们重叠?
子窗口小部件的位置相对于父窗口小部件是相对的,在这种情况下,它是self.toolbar是self.canvas的儿子,因此self.toolbar将在self.canvas内。
为了处理小部件的位置和大小,Qt提供了布局,在这种情况下,这是必要的。另一方面,工具栏不需要位于PlotCanvas类内。最后,在使用QMainWindow时,由于其具有自定义布局,因此不应直接放置这些小部件,因此必须创建一个中央窗口。
import sys, os
from pyface.qt import QtGui, QtCore
os.environ['ETS_TOOLKIT'] = 'qt4'
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
import random
class App(QtGui.QMainWindow):
def __init__(self):
super(App, self).__init__()
self.left = 10
self.top = 10
self.title = 'Minimum, working example'
self.width = 640
self.height = 400
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
centralwidget = QtGui.QWidget()
self.setCentralWidget(centralwidget)
m = PlotCanvas(self, width=5, height=4)
toolbar = NavigationToolbar(m, self)
vbl = QtGui.QVBoxLayout(centralwidget)
vbl.addWidget(toolbar)
vbl.addWidget(m)
self.show()
class PlotCanvas(FigureCanvas):
def __init__(self, parent=None, width=5, height=4, dpi=100):
fig = Figure(figsize=(width, height), dpi=dpi)
self.axes = fig.add_subplot(111)
FigureCanvas.__init__(self, fig)
self.setParent(parent)
self.updateGeometry()
fig.set_tight_layout(True)
self.plot()
def plot(self):
data = [random.random() for i in range(25)]
ax = self.figure.add_subplot(111)
ax.plot(data, 'r-')
ax.set_title("Is this getting overlapped by the toolbar?")
self.draw()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())