From f061422898686506039c6514d71e5f67bbc2a80d Mon Sep 17 00:00:00 2001 From: PySimpleGUI Date: Fri, 9 Oct 2020 16:15:36 -0400 Subject: [PATCH] New vertical layout demo program --- DemoPrograms/Demo_Layout_Vertical.py | 75 ++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 DemoPrograms/Demo_Layout_Vertical.py diff --git a/DemoPrograms/Demo_Layout_Vertical.py b/DemoPrograms/Demo_Layout_Vertical.py new file mode 100644 index 00000000..b34daf34 --- /dev/null +++ b/DemoPrograms/Demo_Layout_Vertical.py @@ -0,0 +1,75 @@ +import PySimpleGUI as sg + +""" + Demo of using the vertical layout parameters and layout helper functions. + Three methods of vertical alignment are shown: + 1. Using Column element to align a single element + 2. Using vtop layout helper function to align a single element + 3. Using vtop layout helper function to align an entire row + + There is also a funciton provided that will convert an entire layout into + a top aligned layout. + + Copyright 2020 PySimpleGUI.org +""" + +def top_align_layout(layout): + """ + Given a layout, return a layout with all rows vertically adjusted to the top + + :param layout: List[List[sg.Element]] The layout to justify + :return: List[List[sg.Element]] The new layout that is all top justified + """ + new_layout = [] + for row in layout: + new_layout.append(sg.vtop(row)) + return new_layout + + +def main(): + # -------------------- Example 1 - No alignment -------------------- + layout = [ [sg.T('This layout uses no vertical alignment. The default is "center"')], + [sg.Text('On row 1'), sg.Listbox(list(range(10)), size=(5,4)), sg.Text('On row 1')], + [sg.Button('OK')] ] + + sg.Window('Window Title', layout).read(close=True) + + # -------------------- Example 2 - Top aligned Text element using Column -------------------- + layout = [ [sg.T('This uses a Column Element to align 1 element')], + [sg.Col([[sg.Text('On row 1')]], vertical_alignment='top', pad=(0,0)), sg.Listbox(list(range(10)), size=(5,4)), sg.Text('On row 1')], + [sg.Button('OK')] ] + + sg.Window('Window Title', layout).read(close=True) + + + # -------------------- Example 3 - Top aligned Text element using Column -------------------- + layout = [ [sg.T('This layout uses the "vtop" layout helper function on 1 element')], + [sg.vtop(sg.Text('On row 1')), sg.Listbox(list(range(10)), size=(5,4)), sg.Text('On row 1')], + [sg.Button('OK')] ] + + sg.Window('Window Title', layout).read(close=True) + + # -------------------- Example 4 - Top align an entire row -------------------- + # Note that the vtop function takes a row as input and returns a row. DO NOT place [ ] around vtop + # because it is a row already. + layout = [ [sg.T('This layout uses the "vtop" layout helper function on 1 row')], + sg.vtop([sg.Text('On row 1'), sg.Listbox(list(range(10)), size=(5,4)), sg.Text('On row 1')]), + [sg.Button('OK')] ] + + sg.Window('Window Title', layout).read(close=True) + + + # -------------------- Example 5 - Use function to align all rows in layout -------------------- + layout = [ [sg.T('This layout has all rows top aligned using function')], + [sg.Text('On row 1'), sg.Listbox(list(range(10)), size=(5,4)), sg.Text('On row 1')], + [sg.Text('On row 2'), sg.Listbox(list(range(10)), size=(5,4)), sg.Text('On row 2')], + [sg.Button('OK')] ] + + layout = top_align_layout(layout) # pass in a layout, get a loyout back + + sg.Window('Window Title', layout).read(close=True) + + + +if __name__ == '__main__': + main() \ No newline at end of file