In the previous post on Python GUI we discussed radio buttons and checkboxes. These elements of pySimpleGUI allow a user to select one or more options from given set of values. In this post we will extend our skill to use two important elements of any GUI interface- Python Combo and Listbox.
Combo
Python Combo element in pySimpleGUI allows displaying a list of values to choose from in an application. The dropdown list of combo is visible when the small down arrow is clicked at the right end of element. The name combo means that you get a DropDown List along with the option to type the value if it is not available in the dropdown List. User can choose only one value from the combo dropdown list or type a value if he is unable to find a value of her choice. The value typed in combo by a user at runtime will not be added to the list of values of combo already available.
Syntax of combo in Python GUI window
Combo( values List, [attributes])
The values to be displayed as options in the combo are added as a list or it can be the name of an existing Python List Variable having stored values.
Attributes
A few commonly used attributes of the combo are-
default_value– String, it is the value that you want the Python combo to store if a user fails to select one. It is the value which is displayed automatically in the text area of the combo and is returned by the read method of the window.
size –(integer,integer), the pair of integer values to define the width and height of the combo.
background_color– String, sets the color of the combo’s background.
text_color-String, sets the color of the text visible in the combo and its dropdown list.
disabled -Boolean, enables or disables user interaction in the dropdown. By default the combo is enabled to let user type or select an option. If disabled the combo is visible but user will not be able to use the combo. Values allowed are True or False.
key/K -String, the key assigned to combo element by which you can identify this element in the values list returned by the window read() method.
tooltip -String, the small text that appears when the user rests mouse cursor on the element.
font -String, to set the font family for combo text.
There are other attributes of the Python Combo that you can read from here
Listbox
Listbox element in pySimple GUI allows you to display a list of values. The Listbox displays a fixed number of values depending on the height of the list. If the values exceed the height, a vertical scroll appears automatically to browse through all the values. User can choose one or more values from the Listbox depending upon the selection mode set. User cannot type any new value, she can only select from the options.
Syntax of using a listbox in Python GUI window
Listbox( values List, [attributes])
The values of the listbox are given in as a list of values or a name of an existing Python List Variable having existing values.
Attributes
A few commonly used attributes of Listbox are-
default_values– List of String values, it is the value that you want the listbox to highlight if a user does not select any values. These are the values that are passed when the read method of window is called.
select_mode– string or constant, defines how the displayed values can be selected by a user. The options as string are browse, extended, multiple or single. The corresponding constants are LISTBOX_SELECT_MODE_BROWSE, LISTBOX_SELECT_MODE_EXTENDED, LISTBOX_SELECT_MODE_MULTIPLE and LISTBOX_SELECT_MODE_SINGLE. Default mode is single – user can select only one value.
size –(integer,integer), the pair of integer values to define the width and height of the Listbox.
background_color-String, sets the color of the listbox background.
text_color-String, sets the color of the text visible in the listbox.
disabled -Boolean, enables or disables user interaction in the dropdown. By default the listbox is enabled to let user type or select an option. If disabled the listbox is visible but user will not be able to use the listbox.
key/K -String, the key given to listbox with which you can identify this element in the values list returned by the window read method.
tooltip -String, the small text that appears when the user rests mouse cursor on the listbox.
font -String, to set the font family for Listbox text
Example – Python Combo and Listbox with pySimpleGui
import PySimpleGUI as psg #set the theme for the screen/window psg.theme('SandyBeach') #define layout layout=[[psg.Text('Choose Boarding place',size=(20, 1), font='Lucida',justification='left')], [psg.Combo(['New York','Chicago','Washington', 'Colorado','Ohio','San Jose','Fresno','San Fransisco'],default_value='Utah',key='board')], [psg.Text('Choose Destination ',size=(30, 1), font='Lucida',justification='left')], [psg.Combo(['New York','Chicago','Washington', 'Colorado','Ohio','San Jose','Fresno','San Fransisco'],key='dest')], [psg.Text('Choose additional Facilities',size=(30, 1), font='Lucida',justification='left')], [psg.Listbox(values=['Welcome Drink', 'Extra Cushions', 'Organic Diet','Blanket', 'Neck Rest'], select_mode='extended', key='fac', size=(30, 6))], [psg.Button('SAVE', font=('Times New Roman',12)),psg.Button('CANCEL', font=('Times New Roman',12))]] #Define Window win =psg.Window('Customise your Journey',layout) #Read values entered by user e,v=win.read() #close first window win.close() #access the selected value in the list box and add them to a string strx="" for val in v['fac']: strx=strx+ " "+ val+"," #display string in a popup psg.popup('Options Chosen', 'You will Travel from :'+ v['board'] + ' to '+v['dest'] +' \nYour additional facilities are:' +strx[1:len(strx)-1] )
Input Screen – No selection in listbox
Input Screen – Multiple selection in listbox
Be First to Comment