如果复选框被选中,在 python 上显示绘图(使用 PyQt4)

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

我是 Python 的新手,我正在尝试使用 PyQt4 编写我的第一个程序。我的问题基本上如下:我的 class 内有两个复选框(Plot1 和 Plot2)和一个 "End" 按钮。当我按下 End 时,我只想看到用户使用 matplotlib 检查的图。我做不到。我的第一个想法是:

        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

当然,这是行不通的,因为 "PlotandEnd" 将绘制两个数字,而不管各自的复选框如何。我怎样才能做我想做的事?

将绘图创建包装在查看复选框状态的 if 语句中。例如:

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

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

您也不需要以下几行:

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

目前这没有任何用处! Qt 从不使用 PlotX() 方法的 return 值,您只希望在单击“结束”按钮时发生事情,而不是在单击复选框时发生。 PlotX() 方法目前仅对您的 PlotandEnd() 方法有用。