简体   繁体   中英

Showing plots if checkbox is checked, on python (with PyQt4)

I'm brand new to Python and I'm trying to make my first program with PyQt4. My problem is basically the following: I have two checkboxes (Plot1 and Plot2), and a "End" push button, inside my class. When I press End, I would like to see only the plots that the user checks, using matplotlib. I'm not being able to do that. My first idea was:

        self.endButton.clicked.connect(self.PlotandEnd)
        self.plot1Checkbox.clicked.connect(self.Plot1)
        self.plot2Checkbox.clicked.conncet(self.Plot2)

    def PlotandEnd(self)
        plot1=self.Plot1()
        pyplot.show(plot1)
        plot2=self.Plot2()
        pyplot.show(plot2)

    def Plot1(self)
        plot1=pyplot.pie([1,2,5,3,2])
        return plot1

    def Plot2(self)
        plot2=pyplot.plot([5,3,5,8,2])
        return plot2

This doesn't work, of course, because "PlotandEnd" will plot both figures, regardless of the respective checkbox. How can I do what I'm trying to?

Wrap the plot creation in an if statement that looks at the state of the check boxes. For example:

def PlotandEnd(self)
    if self.plot1Checkbox.isChecked():
        plot1=self.Plot1()
        pyplot.show(plot1)

    if self.plot2Checkbox.isChecked():
        plot2=self.Plot2()
        pyplot.show(plot2)

You also don't need the following lines:

    self.plot1Checkbox.clicked.connect(self.Plot1)
    self.plot2Checkbox.clicked.conncet(self.Plot2)

This does nothing useful at the moment! Qt never uses the return value of your PlotX() methods, and you only want things to happen when you click the End button, not when you click a checkbox. The PlotX() methods are only currently useful for your PlotandEnd() method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM