简体   繁体   中英

Clearing Date picker in PySimpleGUI

I'm having trouble with the CalendarButton in PySimpleGUI whereby when I clear user input it also clears the title of the CalendarButton. So I'm left with a Calendar button with no title. I only need to clear the user input entries and not anything else like button titles. Any ideas how to get around that?

Below is the section of code that includes the clear function:

layout = [
    [sg.Text('Please fill out the following fields:')],
    [sg.Text('Date',size=(15,1)), sg.InputText(key='Date'),sg.CalendarButton("Select Date",close_when_date_chosen=True, target="Date", format='%Y:%m:%d',size=(10,1))],

    [sg.Text('Service ID', size=(15,1)), sg.InputText(key='Msisdn'),],
    [sg.Text('Account Number', size=(15,1)),sg.InputText(key='Account')],
    [sg.Text('Customer Name', size=(15,1)),sg.InputText(key='Customer')],

    [sg.Submit(), sg.Button('Clear'), sg.Exit()]

]

window = sg.Window('Case entry form', layout)


def clear_input():

    for key in values:
        window[key]('')
    return None

All the items in values are not only for Input elements, but also some other elements, like the CalendarButton element here. It will also update the text of CalendarButton if you call your function clear_input .

Try to specify the condition for which element to be updated.

import PySimpleGUI as sg

layout = [
    [sg.Text('Please fill out the following fields:')],
    [sg.Text('Date', size=(15,1)),            sg.InputText(key='Date'),
     sg.CalendarButton("Select Date", close_when_date_chosen=True, target="Date", format='%Y:%m:%d', size=(10,1))],
    [sg.Text('Service ID', size=(15,1)),     sg.InputText(key='Msisdn'),],
    [sg.Text('Account Number', size=(15,1)), sg.InputText(key='Account')],
    [sg.Text('Customer Name', size=(15,1)),  sg.InputText(key='Customer')],
    [sg.Submit(), sg.Button('Clear'), sg.Exit()]
]

window = sg.Window('Case entry form', layout)

def clear_input(window):
    for key, element in window.key_dict.items():
        if isinstance(element, sg.Input):
            element.update(value='')

while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED:
        break
    print(values)
    if event == 'Clear':
        clear_input(window)

window.close()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM