【发布时间】:2015-01-15 18:38:34
【问题描述】:
我遇到了 PyQt 和 Mathplotlib 的问题。 在这里,您可以找到我正在做的事情的伪代码:我有一个“MainWindow”类,它创建一个带有菜单的主窗口和一个空的 mathplotlib 图。当我单击菜单项时,执行方法“选择”,打开一个新对话框。还有一种方法可以在图形上绘制全局变量 Data 的内容。
import TeraGui
Data = []
class MainWindow(QMainWindow, TeraGui.Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.actionSelect.triggered.connect(self.Select)
# Create the frame with the graphs
self.create_main_frame()
#Plot empty graphs
self.axes.clear()
self.canvas.draw()
def create_main_frame(self):
self.main_frame = QWidget()
# Create the mpl Figure and FigCanvas objects.
# 5x4 inches, 100 dots-per-inch
#
self.dpi = 100
self.fig = Figure((5.0, 4.0), dpi=self.dpi)
self.canvas = FigureCanvas(self.fig)
self.canvas.setParent(self.main_frame)
#
self.axes = self.fig.add_subplot(111)
# Create the navigation toolbar, tied to the canvas
#
self.mpl_toolbar = NavigationToolbar(self.canvas, self.main_frame)
#
# Layout with box sizers
#
vbox = QVBoxLayout()
vbox.addWidget(self.canvas)
vbox.addWidget(self.mpl_toolbar)
self.main_frame.setLayout(vbox)
self.setCentralWidget(self.main_frame)
def Plotting(self):
""" Redraws the figure
"""
print "I am here"
time = Data[0]
sig = Data[]
plot(time, sig)
# clear the axes and redraw the plot anew
#
self.axes.clear()
self.axes.plot(time, sig)
self.canvas.draw()
def Select(self):
dialog = Dialog(self)
dialog.exec_()
现在,如果我在 MainWindow 类的 init 方法中添加这些行:
Global Data
Data = [[1,2,3],[4,5,6]]
self.Plotting()
“我在这里”已打印,并且绘图正确显示在图表中,但是如果我不添加这些行并且我尝试从 Dialog 类调用 Plotting 它将不起作用。 “我在这里”被绘制,但情节保持空白。在Dialog类中,当按下按钮框的“ok”按钮时调用“accept”方法:
class Dialog(QDialog, TeraGui.Ui_SelectFiles):
def __init__(self, parent=None):
super(Dialog, self).__init__(parent)
self.setAttribute(Qt.WA_DeleteOnClose)
self.setupUi(self)
def accept(self):
global Data
Data = [[1,2,3],[4,5,6]]
MainWindow().Plotting()
Plotting 方法还通过命令“plot(time,sig)”绘制单独的图。无论调用 Plotting 的方式如何,此图始终正确显示。
这是我第一次尝试使用 PyQt 和 matplotlib,但我无法识别错误。
【问题讨论】:
标签: matplotlib pyqt