0

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?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

@Thiagogps93 If this answer solved your problem, please consider marking it as the "accepted answer" by clicking on the tick mark to the left of it and/or upvoting the answer.
I made in the accepted answer. I don't have enough reputation to upvote yet, though.

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.