2

我有一个 Python 程序来使用自定义 matplotlib 小部件绘制一个简单的图形。我的代码如下:

import sys
from GUI import *
import random
import matplotlib.pyplot as plt

class GUIForm(QtGui.QDialog):

    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self,parent)
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)

        QtCore.QObject.connect(self.ui.pushButton,
                               QtCore.SIGNAL('clicked()'), self.PlotFunc)

    def PlotFunc(self):
        randValList = random.sample(range(0, 10), 10)
        print(randValList)
        self.ui.PlotWidget.canvas.ax.clear()
        self.ui.PlotWidget.canvas.ax.plot(randValList)

    def callFunc(self):
        myapp.PlotFunc()


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = GUIForm()
    myapp.show()
    sys.exit(app.exec_())

当我运行程序时,我可以看到 GUI,但是当我单击按钮时,绘图不显示线条。空图可见。但是,如果我这样做

def __init__(self, parent=None):
            QtGui.QWidget.__init__(self,parent)
            self.ui = Ui_Dialog()
            self.ui.setupUi(self)

            QtCore.QObject.connect(self.ui.pushButton,
                                   QtCore.SIGNAL('clicked()'), self.PlotFunc)
            self.ui.PlotWidget.canvas.ax.plot(randValList)

或者

if __name__ == "__main__":
        app = QtGui.QApplication(sys.argv)
        myapp = GUIForm()
        myapp.show()
        self.PlotFunc()
        sys.exit(app.exec_())

程序绘制图形。所以我猜它与sys.exit(app.exec_())但不知道如何解决这个问题有关。任何帮助表示赞赏。

谢谢你。

4

1 回答 1

4

无论如何,如果您在 PlotFunc 的末尾添加这一行,它会起作用吗?

self.ui.PlotWidget.canvas.draw()
于 2012-07-22T15:02:44.330 回答