1

I have created a little app in PyQt using QT Designer. I have some buttons, "Plot" button create 4 plots on 4 different Widget. "Clear" button must clear this 4 plots. I try to write some code but it don't run. How can I create this clear button? Thank you.

This is the main of my app:

import sys
from Import_fsa import import_fsa
from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import QFileDialog
from Vannucci_Gemignani import Ui_MainWindow


class GUI_fsa(QtGui.QMainWindow):
    def __init__(self):

        QtGui.QMainWindow.__init__(self)
        self.ui=Ui_MainWindow()
        self.ui.setupUi(self)

        self.ui.Button_Browse.clicked.connect(self.Browse)
        self.ui.Button_Plot.clicked.connect(self.Plot)
        self.ui.Button_Clear.clicked.connect(self.Clear)

    def Browse(self):

        fname=QFileDialog.getOpenFileName()
        if fname=='':  #se non viene selezionato nessun file, fname è
            return     #nullo, generando quindi un errore nell'import fsa

        self.ui.lineEdit.setText(fname)




    def Plot(self):
        if self.ui.lineEdit.text()=='':
            QtGui.QMessageBox.information(None,'Warning','Select fsa File',QtGui.QMessageBox.Ok)
            return

        data_set=import_fsa(self.ui.lineEdit.text())        

        self.ui.widget.canvas.ax.clear()
        self.ui.widget_2.canvas.ax.clear()
        self.ui.widget_3.canvas.ax.clear()
        self.ui.widget_4.canvas.ax.clear()

        self.ui.widget.canvas.ax.plot(data_set[0])
        self.ui.widget_2.canvas.ax.plot(data_set[1])
        self.ui.widget_3.canvas.ax.plot(data_set[2])
        self.ui.widget_4.canvas.ax.plot(data_set[3])

        self.ui.widget.canvas.draw()
        self.ui.widget_2.canvas.draw()
        self.ui.widget_3.canvas.draw()
        self.ui.widget_4.canvas.draw()

    def Clear(self):
        self.ui.widget.canvas.ax.clear()
        self.ui.widget_2.canvas.ax.clear()
        self.ui.widget_3.canvas.ax.clear()
        self.ui.widget_4.canvas.ax.clear()


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    MainWindow = GUI_fsa()
    MainWindow.show()
    sys.exit(app.exec_())
braX
  • 11,506
  • 5
  • 20
  • 33
Gianluca
  • 142
  • 1
  • 4
  • 13
  • 1
    The PyQt code looks fine to me. The button is connected correctly: you can check this yourself by putting `print("clear")` at the top of the `Clear` method. So there must be something wrong with the matplotlib stuff (which I'm not very familiar with). – ekhumoro Mar 25 '14 at 20:20
  • I put print('clear') and, when I click Push Button, I can see 'print' on my Python consolle. So the Push button is running well but the figure is not clear. – Gianluca Mar 26 '14 at 09:38
  • I don't know much about matplotlib, but if you search SO, you should be able to find answers. Try [this question](http://stackoverflow.com/q/8213522/984421), and [this one](http://stackoverflow.com/q/741877/984421). – ekhumoro Mar 26 '14 at 19:20
  • unfortunately my clear Push Button is still not running. It's strange, there are lots of example on the web and all use a code like mine. – Gianluca Mar 27 '14 at 09:23
  • Can you provide a simpler, self-contained example that shows the problem? Your current example cannot be tested, because the `Import_fsa` module is missing. – ekhumoro Mar 27 '14 at 21:32
  • I use this example for create Clear Button [link](http://www.technicaljar.com/?p=688) as you can see the author use this code : self.ui.widget.canvas.ax.clear() – Gianluca Mar 28 '14 at 09:25
  • I was able to create a small test-case and solve the problem: please see my answer. – ekhumoro Mar 30 '14 at 02:22
  • thank all for help, with ekhumoro's code I resolve the problem. – Gianluca Apr 02 '14 at 08:06

1 Answers1

6

It seems that you must re-draw the canvas to clear the plot:

    def Clear(self):
        self.ui.widget.canvas.ax.clear()
        self.ui.widget.canvas.draw()
        self.ui.widget_2.canvas.ax.clear()
        self.ui.widget_2.canvas.draw()
        self.ui.widget_3.canvas.ax.clear()
        self.ui.widget_3.canvas.draw()
        self.ui.widget_4.canvas.ax.clear()
        self.ui.widget_4.canvas.draw()
ekhumoro
  • 115,249
  • 20
  • 229
  • 336