A QCheckBox is a widget that has two states: on and off. It is a box with a label. Checkboxes are typically used to represent features in an application that can be enabled or disabled.
代码:
# coding='utf-8'
from PyQt5.QtWidgets import QApplication, QWidget,\
QCheckBox
from PyQt5.QtCore import Qt
import sys
class Gui(QWidget):
def __init__(self):
super(Gui, self).__init__()
self.start()
def start(self):
# 构造函数: QCheckBox(str, parent: QWidget = None)
# 创建一个QCheckBox
check_box = QCheckBox('展示标题', self)
check_box.move(20, 20)
# toggle的意思是切换,可以理解为选中的意思,\
# 这行代码的意思就是选择check_box-->让它的初始状态是勾选的
check_box.toggle()
# 处理状态改变的事件-->这里self.change_title是slot,
# stateChanged是event target,
# check_box是event source
check_box.stateChanged.connect(self.change_title)
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('单选框')
self.show()
def change_title(self, state):
# QtCore.Qt里面包含了很多有用的用有意义的名字命名好的枚举常量
# state在这里的类型是int, 枚举常量也是int类型的
if state == Qt.Checked:
self.setWindowTitle('单选框')
else:
# 注意着里必须要设置字符串(不能设置成空字符串\
# -->否则窗体自动设置标题为python),\
# 我们设置成单个空格看起来也是没有,效果一样
self.setWindowTitle(' ')
app = QApplication(sys.argv)
gui = Gui()
sys.exit(app.exec_())
运行结果:
image.png
image.png
网友评论