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

如何在PySimpleGUI的popup弹窗中正确显示自定义图片?

实现方案

PySimpleGUI 内置的各类popup快捷函数不支持直接传入image参数加载自定义图片,该参数不会被内置弹窗识别,要实现你要的带图无按钮弹窗效果,推荐直接创建自定义临时窗口模拟弹窗,实现逻辑如下:

完整可运行代码

import PySimpleGUI as sg

# 替换为你的实际参数
img_path = "替换为你的本地图片绝对/相对路径"
popup_title = "Über uns"
display_text = "Text"
text_color = "#F7F6F2"

# 配置弹窗布局,可按需调整间距、对齐方式
layout = [
    [sg.Image(filename=img_path)],
    [sg.Text(display_text, text_color=text_color, pad=(20, 10))]
]

# 创建窗口,继承你之前设置的置顶等参数
window = sg.Window(
    title=popup_title,
    layout=layout,
    keep_on_top=True,
    element_justification="center",
    finalize=True
)

# 监听关闭事件,点击窗口关闭按钮或任意区域均可退出,和无按钮popup逻辑一致
window.read(close=True)

参数调整说明

  • 你可以在sg.Image中传入size=(宽度, 高度)参数固定图片显示尺寸,避免图片过大撑爆窗口
  • 可通过调整sg.Text的font参数修改文字大小、字体
  • 如需调整窗口背景色,可在sg.Window中传入background_color参数统一设置

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 06:57:04