From c3a6db921f14f425dd92f161fa9e08de6882b751 Mon Sep 17 00:00:00 2001 From: PySimpleGUI Date: Fri, 6 Jan 2023 16:01:52 -0500 Subject: [PATCH 1/3] Revert "Added option to enable/disable manual entry" This reverts commit 73a66290456bb8e8e2a85be03b2b6402ed6fd524. --- DemoPrograms/Demo_Time_Chooser.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/DemoPrograms/Demo_Time_Chooser.py b/DemoPrograms/Demo_Time_Chooser.py index 89e20a97..eb36b66e 100644 --- a/DemoPrograms/Demo_Time_Chooser.py +++ b/DemoPrograms/Demo_Time_Chooser.py @@ -16,7 +16,7 @@ import PySimpleGUI as sg -def popup_get_time(title='Time Entry', starting_hour=1, starting_minute=0, allow_manual_input=True, font=None): +def popup_get_time(title='Time Entry', starting_hour=1, starting_minute=0, font=None): """ Shows a window that will gather a time of day. @@ -26,8 +26,6 @@ def popup_get_time(title='Time Entry', starting_hour=1, starting_minute=0, allow :type starting_hour: int :param starting_minute: Value to initially show in the minute field :type starting_minute: int - :param allow_manual_input: If True, then the Spin elements can be manually edited - :type allow_manual_input: bool :param font: Font to use for the window :type font: str | tuple :return: Tuple with format: (hour, minute, am-pm string) @@ -38,9 +36,9 @@ def popup_get_time(title='Time Entry', starting_hour=1, starting_minute=0, allow hour_list = [i for i in range(0, 15)] minute_list = [i for i in range(-1, 62)] - layout = [[sg.Spin(hour_list, initial_value=starting_hour, key='-HOUR-', s=3, enable_events=True, readonly=not allow_manual_input), + layout = [[sg.Spin(hour_list, initial_value=starting_hour, key='-HOUR-', s=3, enable_events=True, readonly=False), sg.Text(':'), - sg.Spin(minute_list, initial_value=starting_minute, key='-MIN-', s=3, enable_events=True, readonly=not allow_manual_input), + sg.Spin(minute_list, initial_value=starting_minute, key='-MIN-', s=3, enable_events=True, readonly=False), sg.Combo(['AM', 'PM'], 'AM', readonly=True, key='-AMPM-')], [sg.Button('Ok'), sg.Button('Cancel')]] From 02f3ae25be5cb2b25881e8878f0f27aa25838b58 Mon Sep 17 00:00:00 2001 From: PySimpleGUI Date: Fri, 6 Jan 2023 16:02:08 -0500 Subject: [PATCH 2/3] Revert "Demo program of an example window to choose a time" This reverts commit 86976f50c3c54c8a2b0741acfaeee457d8367e45. --- DemoPrograms/Demo_Time_Chooser.py | 77 ------------------------------- 1 file changed, 77 deletions(-) delete mode 100644 DemoPrograms/Demo_Time_Chooser.py diff --git a/DemoPrograms/Demo_Time_Chooser.py b/DemoPrograms/Demo_Time_Chooser.py deleted file mode 100644 index eb36b66e..00000000 --- a/DemoPrograms/Demo_Time_Chooser.py +++ /dev/null @@ -1,77 +0,0 @@ -import PySimpleGUI as sg - -""" - Demo Time Chooser - - A sample window for choosing a time. - - This particular implementation uses a Spin element. Numerous possibilities exist for entering a time of day. Instead - of Spin elements, Input or Combo Elements could be used. - - If you do not want your user to be able to manually enter values using the keyboard, then set readonly=True in - the Spin elements. - - Copyright 2023 PySimpleGUI -""" - - - -def popup_get_time(title='Time Entry', starting_hour=1, starting_minute=0, font=None): - """ - Shows a window that will gather a time of day. - - :param title: The title that is shown on the window - :type title: str - :param starting_hour: Value to initially show in the hour field - :type starting_hour: int - :param starting_minute: Value to initially show in the minute field - :type starting_minute: int - :param font: Font to use for the window - :type font: str | tuple - :return: Tuple with format: (hour, minute, am-pm string) - :type: (int, int, str) - """ - - max_value_dict = {'-HOUR-':(1, 12), '-MIN-':(0, 59)} - hour_list = [i for i in range(0, 15)] - minute_list = [i for i in range(-1, 62)] - - layout = [[sg.Spin(hour_list, initial_value=starting_hour, key='-HOUR-', s=3, enable_events=True, readonly=False), - sg.Text(':'), - sg.Spin(minute_list, initial_value=starting_minute, key='-MIN-', s=3, enable_events=True, readonly=False), - sg.Combo(['AM', 'PM'], 'AM', readonly=True, key='-AMPM-')], - [sg.Button('Ok'), sg.Button('Cancel')]] - - window = sg.Window(title, layout, font=font) - - while True: - event, values = window.read() - # print(event, values) - if event == sg.WIN_CLOSED or event == 'Cancel': - hours = minutes = ampm = None - break - - if event == '-HOUR-' or event == '-MIN-': - spin_value = values[event] - if spin_value > max_value_dict[event][1]: - values[event] = max_value_dict[event][0] - window[event].update(values[event]) - elif spin_value < max_value_dict[event][0]: - values[event] = max_value_dict[event][1] - window[event].update(values[event]) - if event == 'Ok': - # Do validation on the input values to ensure they're valid - try: - hours = int(values["-HOUR-"]) - minutes = int(values["-MIN-"]) - ampm = values["-AMPM-"] - except: - continue # if not valid, then don't allow exiting the window using OK. - if 1 <= hours <= 12 and 0 <= minutes < 60: # make sure the hour and minute values are in a valid range - break - - window.close() - - return hours, minutes, ampm - -print(popup_get_time(font='_ 15')) \ No newline at end of file From 0c77b57659d372ef0dc42e6ca58ba89c40074073 Mon Sep 17 00:00:00 2001 From: PySimpleGUI Date: Fri, 6 Jan 2023 16:04:33 -0500 Subject: [PATCH 3/3] Demo Program - Time chooser --- DemoPrograms/Demo_Time_Chooser.py | 79 +++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 DemoPrograms/Demo_Time_Chooser.py diff --git a/DemoPrograms/Demo_Time_Chooser.py b/DemoPrograms/Demo_Time_Chooser.py new file mode 100644 index 00000000..89e20a97 --- /dev/null +++ b/DemoPrograms/Demo_Time_Chooser.py @@ -0,0 +1,79 @@ +import PySimpleGUI as sg + +""" + Demo Time Chooser + + A sample window for choosing a time. + + This particular implementation uses a Spin element. Numerous possibilities exist for entering a time of day. Instead + of Spin elements, Input or Combo Elements could be used. + + If you do not want your user to be able to manually enter values using the keyboard, then set readonly=True in + the Spin elements. + + Copyright 2023 PySimpleGUI +""" + + + +def popup_get_time(title='Time Entry', starting_hour=1, starting_minute=0, allow_manual_input=True, font=None): + """ + Shows a window that will gather a time of day. + + :param title: The title that is shown on the window + :type title: str + :param starting_hour: Value to initially show in the hour field + :type starting_hour: int + :param starting_minute: Value to initially show in the minute field + :type starting_minute: int + :param allow_manual_input: If True, then the Spin elements can be manually edited + :type allow_manual_input: bool + :param font: Font to use for the window + :type font: str | tuple + :return: Tuple with format: (hour, minute, am-pm string) + :type: (int, int, str) + """ + + max_value_dict = {'-HOUR-':(1, 12), '-MIN-':(0, 59)} + hour_list = [i for i in range(0, 15)] + minute_list = [i for i in range(-1, 62)] + + layout = [[sg.Spin(hour_list, initial_value=starting_hour, key='-HOUR-', s=3, enable_events=True, readonly=not allow_manual_input), + sg.Text(':'), + sg.Spin(minute_list, initial_value=starting_minute, key='-MIN-', s=3, enable_events=True, readonly=not allow_manual_input), + sg.Combo(['AM', 'PM'], 'AM', readonly=True, key='-AMPM-')], + [sg.Button('Ok'), sg.Button('Cancel')]] + + window = sg.Window(title, layout, font=font) + + while True: + event, values = window.read() + # print(event, values) + if event == sg.WIN_CLOSED or event == 'Cancel': + hours = minutes = ampm = None + break + + if event == '-HOUR-' or event == '-MIN-': + spin_value = values[event] + if spin_value > max_value_dict[event][1]: + values[event] = max_value_dict[event][0] + window[event].update(values[event]) + elif spin_value < max_value_dict[event][0]: + values[event] = max_value_dict[event][1] + window[event].update(values[event]) + if event == 'Ok': + # Do validation on the input values to ensure they're valid + try: + hours = int(values["-HOUR-"]) + minutes = int(values["-MIN-"]) + ampm = values["-AMPM-"] + except: + continue # if not valid, then don't allow exiting the window using OK. + if 1 <= hours <= 12 and 0 <= minutes < 60: # make sure the hour and minute values are in a valid range + break + + window.close() + + return hours, minutes, ampm + +print(popup_get_time(font='_ 15')) \ No newline at end of file