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

如何实现PySimpleGUI窗口1秒后自动关闭?当前代码无效求解决

解决方案

你的代码里sg.Window(...).read(close=True)未设置超时参数,窗口会一直等待用户触发按钮事件,不会自动关闭。要实现1秒后自动关闭,只需给read()方法加上timeout=1000参数(单位为毫秒,1000即1秒):

修改后的完整代码:

import PySimpleGUI as sg
import pandas as pd

font = ('Arial', 11)  # 原代码中的Areal应为Arial笔误
sg.theme('BrownBlue')

data = Output  # 假设Output是你已定义好的数据源
headings = ['Column1','Column2','Column3']

df = pd.DataFrame(data)
headings = df.columns.tolist()
data = df.values.tolist()

layout = [[sg.Table(data, headings=headings, justification='left', key='-TABLE-')],
          [sg.Button('Run'), sg.Button('Exit')]]

# 添加timeout=1000,让窗口1秒后自动关闭
sg.Window("Overview", layout).read(timeout=1000, close=True)

额外说明:如果需要在自动关闭前仍能响应按钮事件(比如用户点击Exit提前关闭),可以捕获read()的返回值,逻辑如下:

window = sg.Window("Overview", layout)
event, values = window.read(timeout=1000)
window.close()

这样既会在1秒后自动关闭,也能处理用户的按钮操作。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 14:22:03