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

如何在PySimpleGUI中将两个按钮设置为垂直布局?

解决方案

要将两个按钮改为垂直布局,只需把它们放到列容器中(PySimpleGUI里用sg.Column实现),让按钮在列内逐行排列即可。修改后的代码如下:

layout = [
    [
        sg.Input(readonly=True, expand_x=True, key='Main', disabled_readonly_background_color=sg.theme_input_background_color()),
        sg.Button("Select Folder")
    ],
    [
        sg.Frame("Subfolders & Files", frame_subholders), 
        # 把两个按钮放到Column组件里,每个按钮单独占一行
        sg.Column([[sg.Button(' ← ')], [sg.Button(' → ')]], element_justification='center'),
        sg.Frame("Selected files", frame_selected)
    ],
]

关键改动说明

  • 移除了原来平级的两个按钮,改用sg.Column包裹它们
  • sg.Column的参数是一个嵌套列表:每个子列表代表列中的一行,所以[sg.Button(' ← ')]是垂直布局的第一行,[sg.Button(' → ')]是第二行
  • 添加element_justification='center'可以让按钮在列内水平居中,优化视觉效果

如果不需要额外样式,也可以直接用嵌套列表简化写法(效果类似,但灵活性稍差):

layout = [
    [
        sg.Input(readonly=True, expand_x=True, key='Main', disabled_readonly_background_color=sg.theme_input_background_color()),
        sg.Button("Select Folder")
    ],
    [
        sg.Frame("Subfolders & Files", frame_subholders), 
        [[sg.Button(' ← ')], [sg.Button(' → ')]],
        sg.Frame("Selected files", frame_selected)
    ],
]

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 20:11:18