2

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.

4
  • Could you please confirm the name of your button instance? In your connect function you specify self.ui.pushButton. Usually Designer creates them with self.pushButton as default. Commented Jul 18, 2012 at 15:03
  • @dex19dt thank you. It is self.ui because button is part of the Ui_Dialog. Commented Jul 18, 2012 at 15:35
  • Does your print statement show when you click the button? Have you confirmed that the signal is firing at least? Commented Jul 19, 2012 at 3:59
  • @jdi Thank you jdi. Yes I can see the print statement working. Random values are printed everytime I click the button. Commented Jul 19, 2012 at 6:47

1 Answer 1

4

By any chance, does it work if you add this line at the end of PlotFunc?

self.ui.PlotWidget.canvas.draw()
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much! it did work. I didn't know that I need to call draw() on the canvas each time I draw something. I thought app_exec() runs a loop that detects each input and then automatically draws. Thank you!
I found the window has a repaint() method, no arguments. Thank you all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.