Web Analytics Made Easy - Statcounter
Skip to content

Tight Layout with Button States

Saw this example layout written in tkinter and liked it so much I duplicated the interface. It's "tight", clean, and has a nice dark look and feel.

This Recipe also contains code that implements the button interactions so that you'll have a template to build from.

In other GUI frameworks this program would be most likely "event driven" with callback functions being used to communicate button events. The "event loop" would be handled by the GUI engine. If code already existed that used a call-back mechanism, the loop in the example code below could simply call these callback functions directly based on the button text it receives in the window.read call.

timemanagement

    import PySimpleGUI as sg      
    """      
    Demonstrates using a "tight" layout with a Dark theme.      
    Shows how button states can be controlled by a user application.  The program manages the disabled/enabled      
    states for buttons and changes the text color to show greyed-out (disabled) buttons      
    """      

    sg.theme('Dark')      
    sg.set_options(element_padding=(0,0))      

    layout = [[sg.T('User:', pad=((3,0),0)), sg.OptionMenu(values = ('User 1', 'User 2'), size=(20,1)), sg.T('0', size=(8,1))],      
              [sg.T('Customer:', pad=((3,0),0)), sg.OptionMenu(values=('Customer 1', 'Customer 2'), size=(20,1)), sg.T('1', size=(8,1))],      
              [sg.T('Notes:', pad=((3,0),0)), sg.In(size=(44,1), background_color='white', text_color='black')],      
              [sg.Button('Start', button_color=('white', 'black'), key='Start'),      
               sg.Button('Stop', button_color=('white', 'black'), key='Stop'),      
               sg.Button('Reset', button_color=('white', 'firebrick3'), key='Reset'),      
               sg.Button('Submit', button_color=('white', 'springgreen4'), key='Submit')]      
              ]      

    window = sg.Window("Time Tracker", layout, default_element_size=(12,1), text_justification='r', auto_size_text=False, auto_size_buttons=False, default_button_element_size=(12,1), finalize=True)      

    window['Stop'].update(disabled=True)      
    window['Reset'].update(disabled=True)      
    window['Submit'].update(disabled=True)      
    recording = have_data = False      
    while True:      
        event, values = window.read()      
        print(event)      
        if event == sg.WIN_CLOSED:
            exit(69)      
        if event is 'Start':      
            window['Start'].update(disabled=True)      
            window['Stop'].update(disabled=False)      
            window['Reset'].update(disabled=False)      
            window['Submit'].update(disabled=True)      
            recording = True      
         elif event is 'Stop'  and recording:      
            window['Stop'].update(disabled=True)      
            window['Start'].update(disabled=False)      
            window['Submit'].update(disabled=False)      
            recording = False      
            have_data = True      
         elif event is 'Reset':      
            window['Stop'].update(disabled=True)      
            window['Start'].update(disabled=False)      
            window['Submit'].update(disabled=True)      
            window['Reset'].update(disabled=False)      
            recording = False      
            have_data = False      
         elif event is 'Submit'  and have_data:      
            window['Stop'].update(disabled=True)      
            window['Start'].update(disabled=False)      
            window['Submit'].update(disabled=True)      
            window['Reset'].update(disabled=False)      
            recording = False