2

I am very new to PySimpleGUI. I am building a GUI for a desktop application and wanted to use a calendar. But there came a problem with retrieving the value of Calendar Button events in the while loop without timeouts in window.read() when I chose from Calendar Picker Popup. I tried to get its value using event == 'CalendarButton's text', but couldn't, though its button text changes every time if you choose to set a different date. Can you please help with that, or how can I get its (or any element's) value using its key inside the while loop. Or at least can I use Tkinter calendar package. If so, how can I connect Tkinter Calendar with PySimpleGUI window, will I have to use an element's key bindings with Tkinter?

Here's my code for Calendar Button definition which I put into my window's layout:

sg.CalendarButton('Calendar', target='_CALENDAR_', pad=None, font=('MS Sans Serif', 10, 'bold'), 
                button_color=('red', 'white'), key='_CALENDAR_', format=('%d %B, %Y'))

and here's the while loop events handling part

# LOOP EVENTS #
while True:

    # READ EVENT VALUES #
    event, values = window.read()

    # EXIT OR CLOSE EVENT #
    if event is None or event == 'Exit':
        break

    # BUTTON EVENTS

    # CALENDAR BUTTON CLICKED EVENT #
    if event == 'Calendar':
        window['_CALENDAR_'](disabled=True)


# CLOSE WINDOW
window.close()

# POPUP
sg.Popup('Title',
        'The results of the window.',
        'The button clicked was "{}"'.format(event),
        'The values are', values)

Also, I cannot see this event's value in the sg.Popup() output after I exit the window

'EDITED' Sorry, there were errors in sg.Popup(). Now fixed it.

hkam
  • 77
  • 2
  • 8
  • For the popup, you've got 2 errors. The "button clicked" is in the "event" not the values variable. You've got a syntax error on the last line... an extra ' after values. – Mike from PSG Dec 25 '19 at 00:58

1 Answers1

2

The way to both save the value and get the event is to create a hidden input field. Enable events for the input field and you'll get an event when the calendar fills in the input field that you set as the target.

import PySimpleGUI as sg

layout = [  [sg.Text('Calendar example')],
            [sg.In(key='-CAL-', enable_events=True, visible=False), sg.CalendarButton('Calendar', target='-CAL-', pad=None, font=('MS Sans Serif', 10, 'bold'),
                button_color=('red', 'white'), key='_CALENDAR_', format=('%d %B, %Y'))],
            [sg.Exit()]]

window = sg.Window('Calendar', layout)

while True:             # Event Loop
    event, values = window.read()
    print(event, values)
    if event in (None, 'Exit'):
        break
window.close()

Mike from PSG
  • 5,312
  • 21
  • 39
  • 1
    Never used the **enable_events** attribute before, that's cool. That solved the problem very much @MikeyB. One more question is it possible to make a drop-down calendar button, maybe with **sg.InputCombo()**. If yes, then, I would very much appreciate any suggestions on that. Or at least, is it possible to change the layout of the popup calendar window (ways to edit _background color_, _font_, and etc.). – hkam Dec 25 '19 at 10:15
  • These kinds of detailed questions are best answered on the GitHub. Open an Issue. – Mike from PSG Dec 29 '19 at 00:05