分组框控件学习01

1 阅读1分钟

分组框控件QGroupBox学习记录

分组框控件

组合框控件和工具箱控件非常有用,我们可以把它们看作“整理工具”​,能帮助我们把界面变得更加整洁、有序。

from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout, QGroupBox, QLabel, QPushButton
import sys


class MainWindows(QWidget):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.ui()

    def ui(self):
        self.setWindowTitle("组合框控件")
        self.resize(200, 180)
        self.show()

        ####################################
        # 创建主布局和控件布局
        ####################################
        main_layout = QVBoxLayout()
        letter_layout = QVBoxLayout()
        numer_layout = QVBoxLayout()
        ####################################
        # 组合框控件
        ####################################
        self.letter_groupbox = QGroupBox("字母")
        self.letter_groupbox.setCheckable(True)
        self.letter_groupbox.setChecked(True)
        self.number_groupbox = QGroupBox("数值")
        self.number_groupbox.setCheckable(True)
        self.number_groupbox.setChecked(False)
        ####################################
        # 创建控件
        ####################################
        self.label1 = QLabel("a")
        self.label2 = QLabel("b")
        self.label3 = QLabel("c")
        self.button1 = QPushButton("change color")
        self.button1.setCheckable(True)
        self.label4 = QLabel("1")
        self.label5 = QLabel("2")
        self.label6 = QLabel("3")
        # self.label6.setStyleSheet("color: red")
        self.button2 = QPushButton("change color")
        self.button2.setCheckable(True)
        ####################################
        # 将控件添加到垂直布局中
        ####################################
        letter_layout.addWidget(self.label1)
        letter_layout.addWidget(self.label2)
        letter_layout.addWidget(self.label3)
        letter_layout.addWidget(self.button1)
        numer_layout.addWidget(self.label4)
        numer_layout.addWidget(self.label5)
        numer_layout.addWidget(self.label6)
        numer_layout.addWidget(self.button2)

        self.letter_groupbox.setLayout(letter_layout)
        self.number_groupbox.setLayout(numer_layout)
        main_layout.addWidget(self.letter_groupbox)
        main_layout.addWidget(self.number_groupbox)
        self.setLayout(main_layout)

        ####################################
        # 槽函数
        ####################################
        self.button1.clicked.connect(self.button1_function)
        self.button2.clicked.connect(self.button2_function)

    def button1_function(self, checked):
        if checked:
            self.label1.setStyleSheet("color:red")
            self.label2.setStyleSheet("color:blue")
            self.label3.setStyleSheet("color:green")
        else:
            self.label1.setStyleSheet("color:white")
            self.label2.setStyleSheet("color:white")
            self.label3.setStyleSheet("color:white")

    def button2_function(self, checked):
        if checked:
            self.label4.setStyleSheet("color:red")
            self.label5.setStyleSheet("color:blue")
            self.label6.setStyleSheet("color:green")
        else:
            self.label4.setStyleSheet("color:white")
            self.label5.setStyleSheet("color:white")
            self.label6.setStyleSheet("color:white")


if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = MainWindows()
    sys.exit(app.exec())

程序运行效果

Kapture 2025-05-09 at 00.35.49.gif