Umang Bhatia
Umang Bhatia

Reputation: 67

Hiding and Unhiding Text, Input and FileBrowse() in PySimpleGUI

I have made a GUI using PYSimpleGUI which looks something like this

Click here to view GUI

The code for the same is as below:

import PySimpleGUI as sg 

sg.theme('Light Blue 2')

layout = [[sg.Text('Choose files to get started', size=(30, 1), justification='center', font=("Helvetica", 25), relief=sg.RELIEF_RIDGE)],
          [sg.Text('Select Logs you wish to validate', size=(30, 1), justification='left', font=("Helvetica", 15), relief=sg.RELIEF_RIDGE)],
          [sg.Text('_'  * 100, size=(65, 1))], 
          [sg.Checkbox('SVAS Log', size=(10,1), key='chk_svas'),  sg.Checkbox('HSS Log', size=(10,1), key = 'chk_hss'), sg.Checkbox('AOTA Log', size=(10,1), key = 'chk_aota'), sg.Checkbox('Nexus Log', size=(10,1), key = 'chk_nexus')],
          [sg.Button('Get Inputs')],
          [sg.Text('_'  * 100, size=(65, 1))], 
          [sg.Text('Request File', size=(15,1)), sg.Input(key='req'), sg.FileBrowse()],
          [sg.Text('SVAS Log File', size=(15,1), key= 'txt_svas'), sg.Input(key='svas'), sg.FileBrowse(target= 'svas')],
          [sg.Text('HSS Log File', size=(15,1)), sg.Input(key='hss'), sg.FileBrowse()],
          [sg.Text('AOTA Log File', size=(15,1)), sg.Input(key='aota'), sg.FileBrowse()],
          [sg.Text('Nexus Log File', size=(15,1)), sg.Input(key='nexus'), sg.FileBrowse()],
          [sg.Submit('Generate Logs'), sg.Cancel('Quit'), sg.Button('Reset')],
          [sg.Text('Generating Validation Logs...', size=(30,1), visible = False, key = 'progbar_head')],
          [sg.ProgressBar(1000, orientation='h', size=(20, 20), key='progbar', visible = False)]]


window = sg.Window('Provident Logs Validator', layout, size=(600, 400))

while True:
    event, values = window.read()
    if event == 'Quit':
        break
    print(event, values)

Initially the below part should be completely hidden

      [sg.Text('SVAS Log File', size=(15,1), key= 'txt_svas'), sg.Input(key='svas'), sg.FileBrowse(target= 'svas')],
      [sg.Text('HSS Log File', size=(15,1)), sg.Input(key='hss'), sg.FileBrowse()],
      [sg.Text('AOTA Log File', size=(15,1)), sg.Input(key='aota'), sg.FileBrowse()],
      [sg.Text('Nexus Log File', size=(15,1)), sg.Input(key='nexus'), sg.FileBrowse()]

and should toggle visible/invisible selectively depending upon the check boxes I select/deselect. For eg if i only select the check boxes for SVAS and HSS only the below should be made visible:

[sg.Text('SVAS Log File', size=(15,1), key= 'txt_svas'), sg.Input(key='svas'), sg.FileBrowse(target= 'svas')],
          [sg.Text('HSS Log File', size=(15,1)), sg.Input(key='hss'), sg.FileBrowse()],

and if I uncheck them them they should go invisible again

How can this be achieved? I am new to Python and PySimpleGUI.

Upvotes: 3

Views: 12145

Answers (3)

hamidjahandideh
hamidjahandideh

Reputation: 207

this resolve my problem :

text = sg.Text(" in progress", size=(20, 1), enable_events=True, key='-IN-PROGRESS-', font=my_font, visible=False,
        background_color=my_bg_color)

in click :

text.update(visible=True)

Upvotes: 0

St0rml
St0rml

Reputation: 51

Not sure if this is still relevant, but I solved this problem by following this example in the cookbook of PySimpleGUI.

First, create a helper function, which returns a pinned column (I copied this particular code from the link above and expanded it by the bool visible. I also added pad=(0,0) since sg.Column uses a standard indent which we don't want here.) :

def collapse(layout, key, visible):
    """
    Helper function that creates a Column that can be later made hidden, thus appearing "collapsed"
    :param layout: The layout for the section
    :param key: Key used to make this section visible / invisible
    :param visible: visible determines if section is rendered visible or invisible on initialization
    :return: A pinned column that can be placed directly into your layout
    :rtype: sg.pin
    """
    return sg.pin(sg.Column(layout, key=key, visible=visible, pad=(0,0)))

Of course you could also just use the pin command to hide and show the sections, but defining a function makes it easier and prettier. The Column element is a container element that is used to create a layout within your window's layout (source).

Now you define your (sub)-layouts, which you later want to hide.

section1 = [[sg.Text('SVAS Log File', size=(15,1), key= 'txt_svas'), sg.Input(key='svas'), sg.FileBrowse(target= 'svas')]]
...

After you created your sections, you now can create the final layout using the sections and the function collapse. However to hide and show the sections you also need to enable events on the checkboxes like this: [sg.Checkbox('generic_Label', enable_events=True, size=(x,y), key='some_key'), ... There is a section in the cookbook specifically dedicated to this. For your case the layout would look like this:

layout = [[sg.Text('Choose files to get started', size=(30, 1), justification='center', font=("Helvetica", 25), relief=sg.RELIEF_RIDGE)],
         [sg.Text('Select Logs you wish to validate', size=(30, 1), justification='left', font=("Helvetica", 15), relief=sg.RELIEF_RIDGE)],
         [sg.Text('_'  * 100, size=(65, 1))], 
         [sg.Checkbox('SVAS Log', enable_events=True, size=(10,1), key='chk_svas'), sg.Checkbox('HSS Log', enable_events=True, size=(10,1), key = 'chk_hss'), sg.Checkbox('AOTA Log', enable_events=True, size=(10,1), key = 'chk_aota'), sg.Checkbox('Nexus Log', enable_events=True, size=(10,1), key = 'chk_nexus')],
         [sg.Button('Get Inputs')],
         [sg.Text('_'  * 100, size=(65, 1))], 
         [sg.Text('Request File', size=(15,1)), sg.Input(key='req'), sg.FileBrowse()],
         [collapse(section1, 'sec_1', False)],
         [collapse(section2, 'sec_2', False)],
         ... , # Same goes for all the other sections you want to create
         [sg.Submit('Generate Logs'), sg.Cancel('Quit'), sg.Button('Reset')],
         [sg.Text('Generating Validation Logs...', size=(30,1), visible = False, key = 'progbar_head')],
         [sg.ProgressBar(1000, orientation='h', size=(20, 20), key='progbar', visible = False)]]
         
         

With enable_event=True the checkboxes trigger an event when their status changes. The name of the event is the key. The event loop would look like this:

After creating the window with

window = sg.Window('Provident Logs Validator', layout, size=(600, 400))

you need to define some toggle-bools

toggle_sec1 = toggle_sec2 = ... = False # Number of bools depends on number of Checkboxes / Sections you have

. Then you enter the event loop:

while True:
    event, values = window.read()

    if event == 'chk_svas':
        toggle_sec1 = not toggle_sec1
        window['sec_1'].update(visible=toggle_sec1)

    if event == 'chk_hss':
        toggle_sec2 = not toggle_sec2
        window['sec_2'].update(visible=toggle_sec2)

    ...

    if event == 'Quit' or event == sg.WIN_CLOSED:
        break

TL;DR

Define the function

def collapse(layout, key, visible)
    return sg.pin(sg.Column(layout, key=key, visible=visible))

Create sub-layouts for the rows or sections you want to hide or show:

section = [[sg.ElementA(), sg.ElementB(), ...]]
...

Be sure to put in the argument enable_event=True in the checkboxes, which you want to trigger the showing or hiding of given sections.

Create your window layout like you normally would (except the enable_event=True argument for the checkboxes) and add the sections with the collapse function.

layout = [list_of_elements_a,
          list_of_elements_b,
          [sg.Checkbox('Some Label', enable_events=true, key='checkbox_key')],
          collapse(section, 'section_key', False),
          ... ,
          list_of_elements_c
         ]

Create window:

window = sg.Window('Some Title', layout)

Declare toggle booleans:

toggle_bool1 = ... = False

Create event-loop and check if event equals the key of your checkbox:

while True:
    event, values = window.read()

    if event == 'checkbox_key':
        toggle_bool1 = not toggle_bool1
        window['section_key'].update(visible=toggle_bool1)
    
    ...

    if event == 'Quit' or event == sg.WIN_CLOSED:
        break

Upvotes: 5

r-beginners
r-beginners

Reputation: 35115

It's not quite what you want, but there is an easy way to control it.

[sg.Text('SVAS Log File', size=(15,1), key='txt_svas'), sg.Input(key='svas',visible=False, enable_events=True), sg.FileBrowse(key='svas')]

This means that if you click the button when you need it, the input area will open.However, it does affect the layout, so you may not be happy with it. But it's so simple.

Upvotes: 0

Related Questions