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

如何用PySimpleGUI调整导航栏图片显示大小以匹配设计稿?

解决PySimpleGUI导航栏图片仅显示中心部分的问题

你当前代码里给sg.Image设置了size=(100,100),这个参数的作用是指定组件的显示区域尺寸,当图片原始尺寸和该区域不匹配时,PySimpleGUI会自动裁剪图片的中心部分来填充区域,这就是你只看到耳机部分的原因。要完整显示图片,需要调整参数:

解决方案

  • 若要按图片原始尺寸显示,直接移除size参数:
    sg.Image(r'./images/lofigirl.png')
    
  • 若需要等比例缩小/放大图片,使用subsample(按整数比例缩小,比如subsample=2表示缩小为原尺寸的1/2)或scale(PySimpleGUI 4.60及以上版本支持,可设置浮点数比例,比如scale=0.5表示缩小为原尺寸的50%)参数,不要用size强制裁剪。

修改后的完整代码

import PySimpleGUI as sg

# 页面布局
header = [
    [
        # 使用subsample等比例缩放图片,保证完整显示
        sg.Image(r'./images/lofigirl.png', subsample=2),
        sg.Text("App title", size=(10, 1), justification='center', font=("Helvetica", 12)),
        sg.Text("Option 1", size=(10, 1), justification='center', font=("Helvetica", 12)),
        sg.Text("Option 2", size=(10, 1), justification='center', font=("Helvetica", 12)),
    ]
]

content = [
    [
        sg.Text("Content", size=(60, 1), justification='center', font=("Helvetica", 25)),
    ]
]

# 创建窗口
window = sg.Window(
    'Window title',
    header + content,
    location=(100, 100)
)

# 事件循环
while True:
    event, values = window.read(timeout=20)
    
    if event == 'Exit' or event == sg.WIN_CLOSED:
        break

window.close()

补充说明

size参数适合固定组件占位大小,但不适合调整图片显示;subsample和scale是针对图片本身的等比例缩放,能保证图片完整显示且不失真(只要比例设置合适)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 20:15:55