【问题标题】:Showing plots if checkbox is checked, on python (with PyQt4)如果选中复选框,则在 python 上显示图(使用 PyQt4)
【发布时间】:2015-09-29 17:57:03
【问题描述】:

我是 Python 的新手,我正在尝试使用 PyQt4 制作我的第一个程序。我的问题基本上如下:我的班级中有两个复选框(Plot1 和 Plot2)和一个“结束”按钮。当我按下 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”将绘制两个数字,而不管相应的复选框。我该怎么做?

【问题讨论】:

    标签: python matplotlib pyqt


    【解决方案1】:

    将绘图创建包装在查看复选框状态的 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() 方法的返回值,并且您只希望在单击“结束”按钮时发生事情,而不是在单击复选框时发生。 PlotX() 方法目前仅对您的 PlotandEnd() 方法有用。

    【讨论】:

    • 太好了。谢谢。
    • @Thiagogps93 如果此答案解决了您的问题,请考虑通过单击其左侧的勾号和/或投票将其标记为“已接受的答案”。
    • 我已经接受了答案。不过,我还没有足够的声望来投票。
    猜你喜欢
    • 2013-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-14
    • 2015-07-17
    • 2017-02-04
    相关资源
    最近更新 更多