Search code examples
pythonsecuritytabspasswordspysimplegui

Is it possible to secure Tabs in my PySimpleGUI code?


Dears,

Is it possible to secure Tabs in my PySimpleGUI code ? Means that only 1st Tab can be kept accessible and the other ones request password:

Knowing that I'm able to do that using Collapsible function as follows :

def Collapsible(layout, key, title='', arrows=(sg.SYMBOL_DOWN, sg.SYMBOL_UP), 
collapsed=False):

return sg.Column([[sg.T((arrows[1] if collapsed else arrows[0]), enable_events=True,  
text_color='DeepSkyBlue2', k=key+'-BUTTON-'),
                   sg.T(title, enable_events=True,  text_color='yellow', key=key+'-TITLE-')],
                  [sg.pin(sg.Column(layout, key=key, visible=not collapsed, 
metadata=arrows))]], pad=(0,0))

==> Here's teh Layout Part

#### 1st part ####
        [Collapsible(Menu1, SEC1_KEY,, collapsed=True)],
#### 2nd part ####
        [Collapsible(Menu2, SEC2_KEY, collapsed=True)],

while True:             # Event Loop
event, values = window.read()
#print(event, values)

if event == s
 if event.startswith(SEC2_KEY):
            window[SEC2_KEY].update(visible=not window[SEC2_KEY].visible)
        else:
            window[SEC2_KEY+'-BUTTON-'].update(window[SEC2_KEY].metadata[0] if 
            window[SEC2_KEY].visible else window[SEC2_KEY].metadata[1])

Any one can help on that ? Thanks


Solution

  • Information for a question here, IMO, it will be better.

    • Add everything required
    • Remove everything not related.
    • Most simple layout if GUI required.

    Here, just for how to set which tab accessible. tkinter code required here.

    import PySimpleGUI as sg
    
    accessible = [0, 3]
    
    layout = [[sg.TabGroup([[sg.Tab(f'TAB {i}',[[sg.Text(f'This is the Tab {i}')]],key=f'Tab {i}',) for i in range(5)]], key='TabGroup')]]
    window = sg.Window('Tab Group', layout, finalize=True)
    window['TabGroup'].bind('<Button-1>', ' Change', propagate=False)
    
    while True:
    
        event, values = window.read()
    
        if event == sg.WIN_CLOSED:
            break
    
        elif event == 'TabGroup Change':
            e = window['TabGroup'].user_bind_event
            if e.widget.identify(e.x, e.y) == 'label':
                index = e.widget.index(f'@{e.x},{e.y}')
                if index in accessible:
                    window[f'Tab {index}'].select()
                """ Password secured
                else:
                    if sg.popup_get_text("Password") == 'Hello':
                        window[f'Tab {index}'].select()
                """
    
    window.close()