Python 设置qcheckbox的格式
时间: 2024-12-14 15:24:45 浏览: 22
在Python的Qt库中,QCheckBox是一种用于用户选择布尔值(True或False)的控件。如果你想设置QCheckBox的样式,你可以使用`setStyleSheet()`方法。以下是一个简单的例子:
```python
from PyQt5.QtWidgets import QApplication, QWidget, QCheckBox
class MyWidget(QWidget):
def __init__(self):
super().__init__()
# 创建一个QCheckBox
self.check_box = QCheckBox('这是一个复选框')
# 设置样式(例如更改字体颜色、边框等)
style = "QCheckBox::indicator {color: green; border-radius: 4px;} \
QCheckBox::indicator:checked {background-color: lightgreen;}"
self.check_box.setStyleSheet(style)
# 添加到布局中
self.layout = QVBoxLayout()
self.layout.addWidget(self.check_box)
self.setLayout(self.layout)
if __name__ == '__main__':
app = QApplication([])
widget = MyWidget()
widget.show()
app.exec_()
```
在这个例子中,我们设置了未选中时的指示器颜色为绿色,选中时背景色为浅绿色。当然,你可以根据需要自定义其他样式属性。
阅读全文
相关推荐


















