如何清除PyQt中的情节?

时间:2014-03-25 10:28:00

标签: python qt plot pyqt4 clear

我使用QT Designer在PyQt中创建了一个小应用程序。我有一些按钮,“Plot”按钮在4个不同的Widget上创建4个图。 “清除”按钮必须清除这4个图。我尝试编写一些代码,但它没有运行。如何创建此清除按钮?谢谢。

这是我的应用程序的主要内容:

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_())

1 个答案:

答案 0 :(得分:3)

似乎你必须重新绘制画布以清除情节:

    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()