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

如何在PySimpleGUI中为含多组件的行设置整行背景色?

解决方案

可以通过给包含三个Text组件的sg.Column设置统一背景色,并调整组件间距参数来实现整行背景色填充,同时保留独立的Text组件。具体修改如下:

修改后的代码

import PySimpleGUI as sg

layout = [
    [sg.Column(
        [
            [sg.pin(sg.Text('Values', background_color='lightblue', font=('Helvetica 13 bold'), pad=(0,0))), 
             sg.pin(sg.T("$", font=('Helvetica 13 bold'), background_color='lightblue', pad=(0,0))),
             sg.pin(sg.Text("100", background_color='lightblue', pad=(0,0)))]
        ],
        background_color='lightblue',
        expand_x=True,
        pad=(0,0)
    )]
]
window = sg.Window("Title", layout, finalize=True)

while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED:
        break

window.close()

关键调整说明

  • 给外层sg.Column设置background_color='lightblue',让整个Column区域的背景色统一,填充三个Text组件之间的空白区域。
  • 添加expand_x=True参数,让Column自动铺满窗口宽度,实现整行背景色的效果。
  • 给每个Text组件设置pad=(0,0),消除组件自身的内边距,避免组件与Column背景之间出现空隙。
  • Column的pad=(0,0)用于消除Column与窗口边缘的间距(如果需要整行与窗口边缘保留间距,可调整该值,比如pad=(5,0))。

如果需要三个Text组件之间保留少量间距,只需将每个Text的pad改为(2,0)这类非零值即可,Column的背景色会自动填充组件间的空隙。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 02:40:33