From 0178e9b14f35250962f316de7535a42539dea75b Mon Sep 17 00:00:00 2001 From: PySimpleGUI Date: Thu, 3 Nov 2022 08:12:20 -0400 Subject: [PATCH] New Demo Program - shows using objects instead of strings with Listbox --- DemoPrograms/Demo_Listbox_Using_Objects.py | 43 ++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 DemoPrograms/Demo_Listbox_Using_Objects.py diff --git a/DemoPrograms/Demo_Listbox_Using_Objects.py b/DemoPrograms/Demo_Listbox_Using_Objects.py new file mode 100644 index 00000000..8fca4a0d --- /dev/null +++ b/DemoPrograms/Demo_Listbox_Using_Objects.py @@ -0,0 +1,43 @@ +import PySimpleGUI as sg + +""" + Demo - Listbox Using Objects + + Several elements can take not just strings, but objects. The Listsbox is one of them. + This demo show how you can use objects directly in a Listbox in a way that you can access + information about each object that is different than what is shown in the Window. + + The important part of this design pattern is the use of the __str__ method in your item objects. + This method is what determines what is shown in the window. + + Copyright 2022 PySimpleGUI +""" + +class Item(): + def __init__(self, internal, shown): + self.internal = internal + self.shown = shown + + def __str__(self): + return self.shown + +# make list of some objects +my_item_list = [Item(f'Internal {i}', f'shown {i}') for i in range(100)] + +layout = [ [sg.Text('Select 1 or more items and click "Go"')], + [sg.Listbox(my_item_list, key='-LB-', s=(20,20), select_mode=sg.LISTBOX_SELECT_MODE_EXTENDED)], + [sg.Output(s=(40,10))], + [sg.Button('Go'), sg.Button('Exit')] ] + +window = sg.Window('Listbox Using Objects', layout) + +while True: + event, values = window.read() + # print(event, values) + if event == sg.WIN_CLOSED or event == 'Exit': + break + elif event == 'Go': + print('You selected:') + for item in values['-LB-']: + print(item.internal) +window.close()