From f97f933ed0af1f50a7c3e1fdf083d400926b5e66 Mon Sep 17 00:00:00 2001 From: PySimpleGUI Date: Wed, 20 Oct 2021 14:01:55 -0400 Subject: [PATCH] New Demo showing how to make a fixed size column that can have the interior justified. --- ...mo_Column_Fixed_Size_Justified_Elements.py | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 DemoPrograms/Demo_Column_Fixed_Size_Justified_Elements.py diff --git a/DemoPrograms/Demo_Column_Fixed_Size_Justified_Elements.py b/DemoPrograms/Demo_Column_Fixed_Size_Justified_Elements.py new file mode 100644 index 00000000..74d136f2 --- /dev/null +++ b/DemoPrograms/Demo_Column_Fixed_Size_Justified_Elements.py @@ -0,0 +1,77 @@ +import PySimpleGUI as sg + +""" + Columns with a hard coded size that can have elements justified within it. + + The Column element can have the size set to a fixed size, but when doing so, PySimpleGUI has + a limitation that the contents can't be justified using the normal element_justification parameter. + + What to do? + + The Sizer Element to the rescue. + + PySimpleGUI likes to have layouts that size themselves rather than hard coded using a size parameter. The + Sizer Element enables you to create columns with fixed size by making the contents of your column a fixed size. + It is an invisible "padding" type of element. It has a width and a height parameter. + + + Copyright 2021 PySimpleGUI +""" + + +''' +M#"""""""'M dP +## mmmm. `M 88 +#' .M 88d888b. .d8888b. 88 .dP .d8888b. 88d888b. +M# MMMb.'YM 88' `88 88' `88 88888" 88ooood8 88' `88 +M# MMMM' M 88 88. .88 88 `8b. 88. ... 88 88 +M# .;M dP `88888P' dP `YP `88888P' dP dP +M#########M +''' + + +# Let's say this is your layout and you want to center it in a 500 x 300 pixel Column Element. +col_interior = [[sg.Text('My Window')], + [sg.In()], + [sg.In()], + [sg.Button('Go'), sg.Button('Exit'), sg.Cancel(), sg.Ok()]] + +# Intuition would be to write it as this: +layout = [[sg.Text('This layout is broken. The size of the Column is correct, but the elements are not justified')], + [sg.Column(col_interior, element_justification='c', size=(500, 300), background_color='red')]] + +# But when you run it, you'll see that your interior is not centered. + +window = sg.Window('Window Title', layout) + +window.read(close=True) + + +''' +M""MMM""MMM""M dP +M MMM MMM M 88 +M MMP MMP M .d8888b. 88d888b. 88 .dP .d8888b. +M MM' MM' .M 88' `88 88' `88 88888" Y8ooooo. +M `' . '' .MM 88. .88 88 88 `8b. 88 +M .d .dMMM `88888P' dP dP `YP `88888P' +MMMMMMMMMMMMMM +''' + +def ColumnFixedSize(layout, size=(None, None), *args, **kwargs): + # An addition column is needed to wrap the column with the Sizers because the colors will not be set on the space the sizers take + return sg.Column([[sg.Column([[sg.Sizer(0,size[1]-1), sg.Column([[sg.Sizer(size[0]-2,0)]] + layout, *args, **kwargs, pad=(0,0))]], *args, **kwargs)]],pad=(0,0)) + +col_interior = [[sg.Text('My Window')], + [sg.In()], + [sg.In()], + [sg.Button('Go'), sg.Button('Exit'), sg.Cancel(), sg.Ok()]] + + +layout = [[sg.Text('Below is a column that is 500 x 300')], + [sg.Text('With the interior centered')], + [ColumnFixedSize(col_interior, size=(500, 300), background_color='red', element_justification='c', vertical_alignment='t')]] + +window = sg.Window('Window Title', layout) + +window.read(close=True) +