2018-11-13 15:33:55 +00:00
|
|
|
#PySimple examples (v 3.9)
|
2018-09-28 18:57:37 +00:00
|
|
|
#Tony Crewe
|
2018-11-13 15:33:55 +00:00
|
|
|
#Oct 2018 MacOs
|
2018-09-28 18:57:37 +00:00
|
|
|
|
|
|
|
import PySimpleGUI as sg
|
|
|
|
|
2018-11-13 15:33:55 +00:00
|
|
|
#sg.ChangeLookAndFeel('BlueMono')
|
|
|
|
sg.SetOptions (background_color = 'LightBlue',
|
|
|
|
element_background_color = 'LightBlue',
|
|
|
|
text_element_background_color = 'LightBlue',
|
|
|
|
font = ('Calibri', 14, 'bold'),
|
|
|
|
text_color = 'Black',
|
|
|
|
input_text_color ='Black',
|
|
|
|
button_color = ('Black', 'White'))
|
2018-09-28 18:57:37 +00:00
|
|
|
|
|
|
|
#use column feature with height listbox takes up
|
|
|
|
column1 = [
|
2018-11-13 15:33:55 +00:00
|
|
|
[sg.Text('Add or Delete Items\nfrom a Listbox')],
|
|
|
|
[sg.InputText( size = (15,1), key = 'add'), sg.ReadButton('Add', size = (5,1))],
|
|
|
|
[sg.ReadButton('Delete selected entry', size = (18,1))]]
|
2018-09-28 18:57:37 +00:00
|
|
|
|
2018-10-08 05:36:38 +00:00
|
|
|
#initial listbox entries
|
|
|
|
List = ['Austalia', 'Canada', 'Greece']
|
2018-09-28 18:57:37 +00:00
|
|
|
|
|
|
|
#add initial List to listbox
|
|
|
|
layout = [
|
2018-11-13 15:33:55 +00:00
|
|
|
[sg.Listbox(values=[l for l in List], size = (20,8), key ='_listbox_'),
|
2018-09-28 18:57:37 +00:00
|
|
|
sg.Column(column1)]]
|
|
|
|
|
|
|
|
window = sg.Window('Listbox').Layout(layout)
|
|
|
|
|
|
|
|
while True:
|
|
|
|
button, value = window.Read()
|
|
|
|
if button is not None:
|
2018-10-08 05:36:38 +00:00
|
|
|
#value[listbox] returns a list
|
|
|
|
#using value[listbox][0] gives the string
|
|
|
|
if button == 'Delete selected entry':
|
|
|
|
#ensure something is selected
|
|
|
|
if value['_listbox_'] == []:
|
2018-09-28 18:57:37 +00:00
|
|
|
sg.Popup('Error','You must select a Country')
|
|
|
|
else:
|
2018-10-08 05:36:38 +00:00
|
|
|
#find and remove this
|
|
|
|
List.remove(value['_listbox_'][0])
|
2018-09-28 18:57:37 +00:00
|
|
|
if button == 'Add':
|
2018-10-08 05:36:38 +00:00
|
|
|
#add string in add box to list
|
|
|
|
List.append(value['add'])
|
|
|
|
List.sort()
|
2018-09-28 18:57:37 +00:00
|
|
|
#update listbox
|
2018-10-08 05:36:38 +00:00
|
|
|
window.FindElement('_listbox_').Update(List)
|
2018-09-28 18:57:37 +00:00
|
|
|
else:
|
|
|
|
break
|