Search code examples
pythonpysimplegui

How to update a scrollbar in PySimplegui when hiding/unhiding columns?


I have a menu where you can hide/unhide submenus by klicking a checkbox. I want a scrollbar for the column that updates when more options are shown. Here is a mini-example of what I mean:

import PySimpleGUI as sg

hidden_layout =  [[sg.Radio('some more choices 1','choice', default=True, key='ch1')],
                  [sg.Radio('some more choices 2','choice', default=True, key='ch2')],
                  [sg.Radio('some more choices 3','choice', default=True, key='ch3')]]
layout_buttons = [[sg.Radio('some choice 1','choice', default=True, key='ch1')],
                  [sg.Radio('some choice 2','choice', default=True, key='ch2')],
                  [sg.Radio('some choice 3','choice', default=True, key='ch3')],
                  [sg.Checkbox('go bigger',default=False, key='bigger', enable_events=True)],
                  [sg.pin(sg.Column(hidden_layout, key='hidden', visible=False))]]
layout_blah =    [[sg.Multiline(size=(200, 200), font='Courier 10')]]
layout_master =  [[sg.Column(layout_buttons, vertical_alignment='top', scrollable=True,  vertical_scroll_only=True, size=(200,200), key='butons'),
                   sg.Column(layout_blah, vertical_alignment='top')]]
window = sg.Window('Example',layout_master,size=(500,200))
while True:
  event, values = window.read(timeout=100)
  if event in (sg.WIN_CLOSED, 'Exit'):
    break
  elif event == 'bigger':
    window['hidden'].update(visible=values['bigger'])

When you klick the "go bigger" button, more choices show, but you can't klick all of them, since they are outside the window and the scrollbar won't let you scroll down.

column hidden not everything accessible


Solution

  • Method contents_changed of a scrollable Column element

    When a scrollable column has part of its layout changed by making elements visible or invisible or the layout is extended for the Column, then this method needs to be called so that the new scroll area is computed to match the new contents.

    Before you update the content of a scrollable Column, remember to call window.refresh() before it to update the GUI.

      elif event == 'bigger':
        window['hidden'].update(visible=values['bigger'])
        window.refresh()
        window['butons'].contents_changed()