How to add password to Tab in order to open it's content

163 views Asked by At

Dears,

How to add password to Tab in order to open it's content ?

I want to keep Tab1(Mobiles) and Tab3 (Computers) accessible and the other ones request password each time we click on them :

Below is code for the my layout which has 4 Tabs, 2 of them are open :

import PySimpleGUI as sg

accessible = ['Mobiles','Computers']

layout = [[sg.TabGroup([[
sg.Tab('Mobiles', [[sg.Text(f'This is the Tab 1')]],key='Mobiles'), 
sg.Tab('Tabelettes', [[sg.Text(f'This is the 
Tab2')]],key='Tabelettes'),
sg.Tab('Computers',[[sg.Text(f'This is the 
Tab3')]],key='Computers'),
sg.Tab('Televisions',[[sg.Text(f'This is the 
Tab4')]],key='Televisions')]],selected_title_color='yellow', 
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()
        else:
            if sg.popup_get_text("Password") == '2022':
                window[f'Tab {index}'].select()

 window.close()

Thanks in advance

1

There are 1 answers

1
Jason Yang On BEST ANSWER

Just convert the index to the key of tab.

import PySimpleGUI as sg

tabs = ['Mobiles', 'Tabelettes', 'Computers', 'Televisions']
accessible = {'Mobiles','Computers'}

layout = [[sg.TabGroup([[sg.Tab(tab, [[sg.Text(f'This is the Tab {i+1}')]], key=tab) for i, tab in enumerate(tabs)]], selected_title_color='yellow', 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}')
            tab = tabs[index]
            if tab in accessible:
                window[tab].select()
            else:
                if sg.popup_get_text("Password") == '2022':
                    accessible.add(tab)
                    window[tab].select()

window.close()