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

PySimpleGUI中如何将动态生成的按钮右对齐?

PySimpleGUI动态生成按钮右对齐的实现方法

有两种简单的方法可以实现动态生成按钮的右对齐:

方法一:使用sg.push()占位

sg.push()会自动填充左侧空白空间,将按钮挤到窗口右侧,直接在动态按钮列表前添加即可:

words = "S-C-I-E-N-C-E".split('-')
layout = [
    [sg.push()] + [sg.Button(word, size=2) for word in words]
]

方法二:利用sg.Column的右对齐属性

把动态生成的按钮嵌套在sg.Column组件中,通过设置justification='right'让Column内部元素右对齐:

words = "S-C-I-E-N-C-E".split('-')
layout = [
    [sg.Column([[sg.Button(word, size=2) for word in words]], justification='right')]
]

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 05:45:32