简体   繁体   English

在python(使用PyQt4)上显示是否选中复选框的情况

[英]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. 我是Python的新手,正在尝试使用PyQt4编写我的第一个程序。 My problem is basically the following: I have two checkboxes (Plot1 and Plot2), and a "End" push button, inside my class. 我的问题基本上是以下内容:我的课堂上有两个复选框(Plot1和Plot2)和一个“ End”按钮。 When I press End, I would like to see only the plots that the user checks, using matplotlib. 当我按End键时,我只想查看用户使用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. 当然,这是行不通的,因为“ PlotandEnd”将绘制两个图形,而与各个复选框无关。 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. 将绘图创建内容包装在if语句中,该语句查看复选框的状态。 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. Qt从不使用PlotX()方法的返回值,并且您只希望单击“结束”按钮时发生事情,而不希望单击复选框。 The PlotX() methods are only currently useful for your PlotandEnd() method. PlotX()方法当前仅对您的PlotandEnd()方法有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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