pyqtcheckbox设置选中状态
时间: 2023-08-21 08:03:47 浏览: 89
要设置 PyQt 中的 QCheckBox 组件的选中状态,可以使用setChecked() 方法。该方法接受一个布尔值参数,True 表示选中,False 表示未选中。
下面是一个例子:
```python
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QCheckBox
import sys
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
layout = QVBoxLayout()
checkbox = QCheckBox('选中')
checkbox.setChecked(True) # 设置选中状态
layout.addWidget(checkbox)
self.setLayout(layout)
self.setWindowTitle('设置选中状态')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
```
在上面的例子中,我们创建了一个名为 Example 的窗口,并添加了一个 QCheckBox 组件。在 initUI() 方法中,我们调用了 setChecked(True) 方法来设置 QCheckBox 的选中状态为选中。