Web Analytics Made Easy - Statcounter
Skip to content

Column

Column call reference

import PySimpleGUI as sg

column_layout1 = [[sg.Button('My first Button!'), sg.Checkbox('My first Checkbox!')],
                  [sg.Button('Another Button.')]]
column_layout2 = [[sg.Button('My third Button!'), sg.Checkbox('My second Checkbox!')]]

layout = [[sg.Column(column_layout1, element_justification='center'), sg.Column(column_layout2, element_justification='center')]]

Columns and Frames and Tabs are all "Container Elements" and behave similarly. This section focuses on Columns but the container concept applies to multiple elements.

A container element enables you to embed a layout within a layout. Without this embedding capability it would not be possible to design complex layouts.

Column layouts are specified, like all "container elements", in exactly the same way as a window, as a list of lists.

One simple example easily demonstrates why a Column element is needed. The desired layout is a Listbox followed by 2 rows of text.

image

Without a Column element, it's not possible, but using a Column element, it's easy. Stating the desired problem to solve gives you the solution:

A Listbox element followed by 3 rows of Text elements.

The only way to get 3 rows of Text elements onto a row with something else is by embedding them in the layout using a container element.

import PySimpleGUI as sg

layout = [  [sg.Text('Why a Column is needed')],
            [sg.Listbox(list(range(10)), size=(5, 10), key='-LB-'),
             sg.Column([[sg.Text('Row 1')], [sg.Text('Row 2')], [sg.Text('Row 3')]])],
            [sg.Button('Exit')]  ]

sg.Window('Column Element', layout).read(close=True)

Events

Column elements do not generate any events.

Values Dictionary

There is no values dictionary entry for Column elements.

Padding of (0,0) with Column Elements

Remember that all elements have padding. If you want an "invisible" Column, that is to say you do not wish the Column element to add its padding to the layout, then set the padding to (0,0) for the Column. This is a very common construct in PySimpleGUI.

Let's make a window that has 2 buttons followed by 3 buttons in an arrangement like this:

image

import PySimpleGUI as sg

layout = [  [sg.Text('Why a Column is needed')],
            [sg.Column([[sg.Button('Row 1', p=0, border_width=0)], [sg.Button('Row 2', p=0, border_width=0)]], p=0),
             sg.Column([[sg.Button('Row 1', p=0, border_width=0)], [sg.Button('Row 2', p=0, border_width=0)], [sg.Button('Row 3', p=0, border_width=0)]], p=0)],
            [sg.Button('Exit')]  ]

sg.Window('Column Element', layout, use_default_focus=False).read(close=True)

Notice that the Buttons and the Columns all have 'p=0(which is the same as writingpad=(0,0)`. If only the Buttons had padding of 0,0 then you would get a window that looks like this:

image

import PySimpleGUI as sg

layout = [  [sg.Text('Why a Column is needed')],
            [sg.Column([[sg.Button('Row 1', p=0, border_width=0)], [sg.Button('Row 2', p=0, border_width=0)]]),
             sg.Column([[sg.Button('Row 1', p=0, border_width=0)], [sg.Button('Row 2', p=0, border_width=0)], [sg.Button('Row 3', p=0, border_width=0)]])],
            [sg.Button('Exit')]  ]

sg.Window('Column Element', layout, use_default_focus=False).read(close=True)

The Buttons are all flush with each other within each Column, but each of the Column element is adding padding which is why there is a space between the 2 sets of Buttons.

Columns As a Way to Modify Elements

There are many many uses of Column elements, as can be seen by looking at the Column parameters. The Column can have a single element. This can be useful for performing operations such as justification. Justifying a Listbox to be centered on a row while other rows are not centered can be accomplished by placing the Listbox in a Column and setting the element_justification parameter as well as the expand_x parameter so that it will expand to fill the window's size..

image

import PySimpleGUI as sg

layout = [  [sg.Text('Centering a single element')],
            [sg.Column([[sg.Listbox(list(range(10)), size=(5, 10), key='-LB-')]], element_justification='c', expand_x=True)],
            [sg.Button('Exit')]  ]

sg.Window('Column Element', layout).read(close=True)