Python 在 pyqt 中使用复选框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30462078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:28:02  来源:igfitidea点击:

Using a checkbox in pyqt

pythoncheckboxpyqt

提问by MapEngine

1) I have a checkbox called "ch_check" in my UI created with Qt designer that needs to be tested

1) 我在用 Qt 设计器创建的 UI 中有一个名为“ch_check”的复选框,需要进行测试

2) There is also a button, "bt_button", which triggers a simple function:

2)还有一个按钮“bt_button”,它触发了一个简单的功能:

self.dlg.bt_button.clicked.connect(self.doCheck)

3) The function:

3)功能:

def doCheck(self):
    if ch_check.isChecked():
        self.dlg.le_text.setText("Hello")
    else:
        self.dlg.le_text.setText("Nope")

However I can't figure out how to reference the box properly. How would I do that? Do I need to connect the checkbox somehow first? All the examples I found so far use checkboxes to fire off functions and whatnot while completely ignoring this basic usage. I found this question but it's not answering how to address the existing checkbox: How to check if a checkbox is checked in pyqt

但是我无法弄清楚如何正确引用该框。我该怎么做?我需要先以某种方式连接复选框吗?到目前为止,我发现的所有示例都使用复选框来触发函数等,而完全忽略了这种基本用法。我发现了这个问题,但它没有回答如何解决现有的复选框:How to check if a checkbox is checked in pyqt

采纳答案by MapEngine

As mentioned in the comment above, I just made a small mistake. The correct code would be:

正如上面评论中提到的,我只是犯了一个小错误。正确的代码是:

def doCheck(self):
    checker = self.dlg.ch_check
    if self.dlg.ch_check.isChecked():
        self.dlg.le_text.setText("Hello")
    else:
        self.dlg.le_text.setText("Nope")

回答by Andy

You can do this utilizing the StateChangedsignal. For this example we have a simple .uiand a simple .pyfile:

您可以使用StateChanged信号执行此操作。对于这个例子,我们有一个简单.ui.py文件:

The .uifile defines two widgets. A check box (ch_check) and a single QLabel (my_label)

.ui文件定义了两个小部件。一个复选框 ( ch_check) 和一个 QLabel ( my_label)

The python file:

蟒蛇文件:

from PyQt4 import QtCore
from PyQt4 import QtGui 
import sys
from test_ui import Ui_MainWindow

class CheckDialog(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QWidget.__init__(self)

        # Set up the user interface from Designer.
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.ui.ch_check.stateChanged.connect(self.state_changed)

    def state_changed(self, int):
        if self.ui.ch_check.isChecked():
            self.ui.my_label.setText("CHECKED!")
        else:
            self.ui.my_label.setText("UNCHECKED!")


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    window = CheckDialog()
    window.show()
    sys.exit(app.exec_())


Explanation:

解释:

We set up our signal with this line:

我们用这条线设置我们的信号:

self.ui.ch_check.stateChanged.connect(self.state_changed)

When the state of the checkbox changes, it will call the state_changedfunction. This is where your logic to check whether the box is checked or unchecked goes.

当复选框的状态发生变化时,它会调用该state_changed函数。这是您检查复选框是选中还是未选中的逻辑的地方。

def state_changed(self, int):
    if self.ui.ch_check.isChecked():
        self.ui.my_label.setText("CHECKED!")
    else:
        self.ui.my_label.setText("UNCHECKED!")

In the function, we determine if the check box has been checked. If so, we change our label to say "CHECKED", if it is unchecked the label changes to "UNCHECKED".

在函数中,我们确定复选框是否已被选中。如果是这样,我们将标签更改为“已检查”,如果未选中,则标签将更改为“未检查”。



Example:

例子:

When the application is launched the UI looks like this:

当应用程序启动时,用户界面如下所示:

Newly opened app

新打开的应用

Checking the box, changes the label:

选中该框,更改标签:

Checked checkbox

Checked checkbox

Unchecking the box, also changes the label:

取消选中该框,也会更改标签:

Unchecked checkbox

Unchecked checkbox