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

PySimpleGUI表单提交后如何保留CalendarButton的按钮文本?

解决CalendarButton按钮文本被清空的问题

你的clear_input函数会遍历所有控件对应的key,并将它们的内容设为空字符串,这就导致CalendarButton的按钮文本也被清空了。可以通过以下两种方式避免这个问题:

方法一:明确跳过CalendarButton的key

如果已经知道CalendarButton对应的key(比如设为'-CALENDAR-'),直接在循环里跳过这个key即可:

def clear_input():
    # 替换成你的CalendarButton实际key
    skip_keys = {'-CALENDAR-'}
    for key in values:
        if key not in skip_keys:
            window[key]('')
    return None

方法二:根据控件类型选择性清空

如果不确定key或者需要适配更多控件,可以通过判断控件类型,只清空输入类控件(如输入框、多行文本框等),跳过CalendarButton这类按钮控件:

def clear_input():
    for key in values:
        elem = window[key]
        # 只针对输入类控件执行清空操作
        if elem.Type in ('InputText', 'Multiline', 'InputCombo', 'InputPassword'):
            elem('')
    return None

第一种方法简单直接,适合控件结构固定的场景;第二种方法更灵活,能自动适配新增的输入类控件,不会误操作按钮类控件。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 20:32:01