You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PyQT中如何缩小单选按钮组的垂直间距?

缩小PyQt单选按钮组的垂直间距

我正尝试缩小PyQt中一组单选按钮的垂直间距,但遇到了困难。我希望图中的三个选项在垂直方向上能更紧凑。

(图示:三个单选按钮垂直排列,当前间距较大)

问题复现代码

class MyPopUp(QWidget):
    def __init__(self, task=None):
        super(MyPopUp, self).__init__()
        self.task = task

        self.setMinimumSize(400,600)

        self.populate_fields()

    def populate_fields(self):

        self.task_popup_layout = QVBoxLayout()
        self.setLayout(self.task_popup_layout)

        # 任务完成时间设置标题
        self.scheduling_header_label = QLabel("<h3>Getting it done</h3>")
        self.task_popup_layout.addWidget(self.scheduling_header_label)

        # 截止日期选择
        self.deadline_layout = QHBoxLayout()
        self.deadline_date_label = QLabel("When does it need to be complete by?")
        self.deadline_layout.addWidget(self.deadline_date_label)
        self.deadline_dateedit = QDateEdit(calendarPopup=True)
        self.deadline_dateedit.setAlignment(Qt.AlignmentFlag.AlignRight)
        self.deadline_dateedit.setDateTime(datetime.now() + timedelta(days=7))
        self.deadline_layout.addWidget(self.deadline_dateedit)
        self.task_popup_layout.addLayout(self.deadline_layout)

        # 截止日期类型选择
        self.deadline_type_layout = QHBoxLayout()
        self.task_popup_layout.addLayout(self.deadline_type_layout)
        self.deadline_type_label = QLabel("How strict is the deadline")
        self.deadline_type_label.setWordWrap(True)
        self.deadline_type_layout.addWidget(self.deadline_type_label)

        self.deadline_group_layout = QVBoxLayout()
        
        self.deadline_type_layout.addLayout(self.deadline_group_layout)
       
        self.deadline_group = QButtonGroup()
        self.soft_deadline_radio = QRadioButton("Target Deadline")
        self.deadline_group_layout.addWidget(self.soft_deadline_radio)
        self.deadline_group.addButton(self.soft_deadline_radio)
        self.hard_deadline_radio = QRadioButton("Hard Deadline")
        self.hard_deadline_radio.setChecked(True)
        self.deadline_group_layout.addWidget(self.hard_deadline_radio)
        self.deadline_group.addButton(self.hard_deadline_radio)
        self.specific_deadline_radio = QRadioButton("Must be done on this exact day")
        self.deadline_group_layout.addWidget(self.specific_deadline_radio)
        self.deadline_group.addButton(self.specific_deadline_radio)

解决方法

1. 调整布局的间距与边距

直接修改容纳单选按钮的QVBoxLayout参数,缩小控件间间隔和布局外边距:

self.deadline_group_layout = QVBoxLayout()
# 设置控件间垂直间距(单位:像素,数值越小越紧凑)
self.deadline_group_layout.setSpacing(5)
# 设置布局上下左右的外边距,可按需调小上下边距
self.deadline_group_layout.setContentsMargins(0, 0, 0, 0)

2. 压缩单选按钮自身内边距

如果布局调整后仍不满意,通过样式表缩小每个单选按钮的上下内边距:

# 统一设置样式,避免重复代码
radio_style = "QRadioButton { padding-top: 2px; padding-bottom: 2px; }"
self.soft_deadline_radio.setStyleSheet(radio_style)
self.hard_deadline_radio.setStyleSheet(radio_style)
self.specific_deadline_radio.setStyleSheet(radio_style)

3. 结合两种方式效果更佳

同时调整布局间距和控件内边距,能达到最理想的紧凑效果:

self.deadline_group_layout = QVBoxLayout()
self.deadline_group_layout.setSpacing(3)
self.deadline_group_layout.setContentsMargins(0, 0, 0, 0)

radio_style = "QRadioButton { padding-top: 1px; padding-bottom: 1px; }"
for btn in [self.soft_deadline_radio, self.hard_deadline_radio, self.specific_deadline_radio]:
    btn.setStyleSheet(radio_style)

内容的提问来源于stack exchange,提问作者peetysmith

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 20:40:50