106.md 1.4 KB
Newer Older
W
init  
wizardforcel 已提交
1 2 3 4 5 6 7 8 9 10
# q 复选框

> 原文: [https://pythonbasics.org/qcheckbox/](https://pythonbasics.org/qcheckbox/)

复选框( **QCheckbox** )是默认的 qt 小部件,可以使用方法`setChecked(true)`进行检查。 它是`PyQt5.QtWidgets`的一部分。

您可以使用`.toggled.connect()`添加点击回调/信号。 然后,接收插槽可以处理事件。

![pyqt checkox](img/c05561472bf0335f0774e08359499538.jpg)

W
wizardforcel 已提交
11

W
init  
wizardforcel 已提交
12 13 14 15 16 17 18 19
[带有 Python 的 PyQt 桌面应用程序](https://gum.co/pysqtsamples)

## QCheckBox

### PyQt 复选框示例

下面的示例在 PyQt 窗口中创建一个复选框。 如果单击复选框,它将调用方法`onClicked()`。 方法`.isChecked()`可用于获取复选框状态。

W
wizardforcel 已提交
20
```py
W
init  
wizardforcel 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
from PyQt5.QtWidgets import *
import sys

class Window(QWidget):
    def __init__(self):
        QWidget.__init__(self)
        layout = QGridLayout()
        self.setLayout(layout)

        radiobutton = QCheckBox("I have a Cat")
        radiobutton.setChecked(True)
        radiobutton.animal = "Cat"
        radiobutton.toggled.connect(self.onClicked)
        layout.addWidget(radiobutton, 0, 0)

    def onClicked(self):
        radioButton = self.sender()
        print("Animal " + (radioButton.animal) + " is " + str(radioButton.isChecked()))

app = QApplication(sys.argv)
screen = Window()
screen.show()
sys.exit(app.exec_())

```

[下载示例](https://gum.co/pysqtsamples)