1. 코드

from PyQt5.QtWidgets import QWidget, QApplication, QGroupBox, QRadioButton, QCheckBox, QPushButton, QGridLayout, QVBoxLayout, QMenu, QLabel, QHBoxLayout
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap
import sys

class 그룹박스(QWidget):

    def __init__(self):
        super().__init__()
        self.UI초기화()

    def UI초기화(self):
        img = QPixmap('img/pyqt.png')  
        label = QLabel()
        label.setPixmap(img) 
        label.setAlignment(Qt.AlignCenter)

        grid = QGridLayout()
        grid.addWidget(self.RadioGroup(), 0, 0)
        grid.addWidget(self.CheckGroup(), 1, 0)
        grid.addWidget(label, 0, 1, 2, 1)
        grid.addWidget(self.PushButtonGroup(), 2, 0, 2, 2)

        self.setLayout(grid)

        self.setWindowTitle('QGroupBox')
        self.setGeometry(300, 300, 480, 320)
        self.show()

    def RadioGroup(self):
        RadioGroupBox = QGroupBox('라디오 버튼 그룹')

        radio1 = QRadioButton('Radio 버튼 1')
        radio2 = QRadioButton('Radio 버튼 2')
        radio3 = QRadioButton('Radio 버튼 3')
        radio2.setChecked(True) 

        vbox = QVBoxLayout()
        vbox.addWidget(radio1)
        vbox.addWidget(radio2)
        vbox.addWidget(radio3)
        RadioGroupBox.setLayout(vbox)

        return RadioGroupBox  

    def CheckGroup(self):
        CheckBoxGroup = QGroupBox('체크박스 그룹')
        CheckBoxGroup.setCheckable(True)  
        CheckBoxGroup.setChecked(False)

        checkbox1 = QCheckBox('체크박스1')
        checkbox1.setChecked(True)
        checkbox2 = QCheckBox('체크박스2')

        tristatebox = QCheckBox('체크박스3')
        tristatebox.setTristate(True)

        hbox = QHBoxLayout()

        hbox.addWidget(checkbox1)
        hbox.addWidget(checkbox2)
        hbox.addWidget(tristatebox)
        CheckBoxGroup.setLayout(hbox)

        return CheckBoxGroup

    def PushButtonGroup(self):
        PushButtonGroupBox = QGroupBox('푸시버튼 그룹')
        PushButtonGroupBox.setAlignment(Qt.AlignCenter)
       
        PushButton = QPushButton('기본 버튼') 
        PushButton.setStyleSheet("color: green;"
                          "border-style: solid;"
                          "border-width: 3px;"
                          "background-color: beige;"
        )

        CheckedButton = QPushButton('체크 표시 버튼')
        CheckedButton.setCheckable(True) 
        CheckedButton.setChecked(True)  

        FlatButton = QPushButton('Flat 버튼')
        FlatButton.setFlat(True)

        PopupButton = QPushButton('팝업 창 버튼') 
        menu = QMenu(self) 
        menu.addAction('옵션 1')
        menu.addAction('옵션 2')
        PopupButton.setMenu(menu) 

        vbox = QVBoxLayout()
        vbox.addWidget(PushButton)
        vbox.addWidget(CheckedButton)
        vbox.addWidget(FlatButton)
        vbox.addWidget(PopupButton)
        PushButtonGroupBox.setLayout(vbox)

        return PushButtonGroupBox

프로그램무한반복 = QApplication(sys.argv)
실행인스턴스 = 그룹박스()
프로그램무한반복.exec_()

2. 상세 내용

그룹 박스는 여러가지 Object, 가령 Label이나 PushButton 등을 모아서 하나의 그룹을 만들 수 있는 기능입니다. QGroupBox를 통해 만들 수 있습니다. 여기서는 각각의 그룹을 모듈화 하여 만들도록 하겠습니다.

def RadioGroup(self):
    RadioGroupBox = QGroupBox('라디오 버튼 그룹')

    radio1 = QRadioButton('Radio 버튼 1')
    radio2 = QRadioButton('Radio 버튼 2')
    radio3 = QRadioButton('Radio 버튼 3')
    radio2.setChecked(True)

    vbox = QVBoxLayout()
    vbox.addWidget(radio1)
    vbox.addWidget(radio2)
    vbox.addWidget(radio3)
    RadioGroupBox.setLayout(vbox)

    return RadioGroupBox
def CheckGroup(self):
    CheckBoxGroup = QGroupBox('체크박스 그룹')
    CheckBoxGroup.setCheckable(True)
    CheckBoxGroup.setChecked(False)

    checkbox1 = QCheckBox('체크박스1')
    checkbox1.setChecked(True)
    checkbox2 = QCheckBox('체크박스2')

    tristatebox = QCheckBox('체크박스3')
    tristatebox.setTristate(True)

    hbox = QHBoxLayout()

    hbox.addWidget(checkbox1)
    hbox.addWidget(checkbox2)
    hbox.addWidget(tristatebox)
    CheckBoxGroup.setLayout(hbox)

    return CheckBoxGroup
def PushButtonGroup(self):
    PushButtonGroupBox = QGroupBox('푸시버튼 그룹')
    PushButtonGroupBox.setAlignment(Qt.AlignCenter)

    PushButton = QPushButton('기본 버튼')
    PushButton.setStyleSheet("color: green;"
                             "border-style: solid;"
                             "border-width: 3px;"
                             "background-color: beige;"
                             )

    CheckedButton = QPushButton('체크 표시 버튼')
    CheckedButton.setCheckable(True)
    CheckedButton.setChecked(True)

    FlatButton = QPushButton('Flat 버튼')
    FlatButton.setFlat(True)

    PopupButton = QPushButton('팝업 창 버튼')
    menu = QMenu(self)
    menu.addAction('옵션 1')
    menu.addAction('옵션 2')
    PopupButton.setMenu(menu)

    vbox = QVBoxLayout()
    vbox.addWidget(PushButton)
    vbox.addWidget(CheckedButton)
    vbox.addWidget(FlatButton)
    vbox.addWidget(PopupButton)
    PushButtonGroupBox.setLayout(vbox)

    return PushButtonGroupBox