Reputation: 5450
I have a Python program to plot a simple graph in using a custom matplotlib widget. My code is as follows:
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_())
When I run the program I can see the GUI but when I click the button the plot does not show the line. The empty plot is visible. However if I dO
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)
or
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = GUIForm()
myapp.show()
self.PlotFunc()
sys.exit(app.exec_())
the program draws the graph.
So I'm guessing it has to do with sys.exit(app.exec_())
but do not know how to fix this.
Any help is appreciated.
Thank you.
Upvotes: 2
Views: 3394
Reputation: 4718
By any chance, does it work if you add this line at the end of PlotFunc?
self.ui.PlotWidget.canvas.draw()
Upvotes: 4