QCheckBox 是 PyQt6 中的一个复选框控件,它允许用户通过单击来选择或取消选择某个选项。与 QRadioButton 不同,QCheckBox 控件并不互斥,这意味着用户可以同时选择多个 QCheckBox。示例对应的制作的 ui文件 界面如下所示。
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Form</class>
 <widget class="QWidget" name="Form">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>300</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <widget class="QWidget" name="verticalLayoutWidget">
   <property name="geometry">
    <rect>
     <x>20</x>
     <y>20</y>
     <width>101</width>
     <height>80</height>
    </rect>
   </property>
   <layout class="QVBoxLayout" name="verticalLayout">
    <item>
     <widget class="QCheckBox" name="checkBox_2">
      <property name="text">
       <string>选项1</string>
      </property>
     </widget>
    </item>
    <item>
     <widget class="QCheckBox" name="checkBox_1">
      <property name="text">
       <string>选项2</string>
      </property>
     </widget>
    </item>
    <item>
     <widget class="QCheckBox" name="checkBox_3">
      <property name="text">
       <string>选项3</string>
      </property>
     </widget>
    </item>
   </layout>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>
 

from PyQt6.QtWidgets import QApplication, QMainWindow, QButtonGroup
from PyQt6.uic import loadUi
from PyQt6.QtCore import Qt 
  
class MyWindow(QMainWindow):  
    def on_checkbox_changed(self, state):  
        # 槽函数,当复选框状态改变时调用  
        sender = self.sender()  # 获取发出信号的控件
        if state:  
            print(f'选中的复选框是: {sender.text()}')  
        else:  
            print(f'取消选中的复选框是: {sender.text()}')
    def __init__(self, ui_file):    
        super().__init__()    
        # 使用 loadUi 加载 .ui 文件    
        loadUi(ui_file, self)    
    
        # 初始化窗口设置(如果需要)    
        self.setWindowTitle('My Window')
        
        # 连接每个 QCheckBox 的 stateChanged 信号到槽函数  
        self.checkBox_1.stateChanged.connect(self.on_checkbox_changed)  
        self.checkBox_2.stateChanged.connect(self.on_checkbox_changed)  
        self.checkBox_3.stateChanged.connect(self.on_checkbox_changed)  
  
if __name__ == '__main__':    
    import sys    
    app = QApplication(sys.argv)    
    
    # 假设 untitled.ui 是你的 UI 文件,并且文件在同一目录    
    window = MyWindow('untitled.ui')    
    window.show()    
    
    sys.exit(app.exec())
 
                





![[SWPUCTF 2022 新生赛]奇妙的MD5... ...](https://img-blog.csdnimg.cn/direct/ac722a61490945769341258a522f298a.png)












