Lists in GUI4sher
The GUI4sher list widget allows the project to display a python list (enclosed with []s) and react to the user selecting items in the list. You use place_list to put a list on a GUI4sher project. It requires a python list as an argument. A list can contain a combination of strings and numbers. Usually you want a list of strings.The example below allows the user to select their age range:
A function with the name of the list followed by select tells the project what to do when the user selects a member of the list. The method selected returns the list member selected by the user.
This is what selecting looks like:
Widgets in your project can modify a list. This is an example of a list of numbers:
When the user puts an integer in the entry (top middle):
And then when the user hits enter, the number is added to the list and the sum is updated:
Putting a 7 in the list makes the sum 12:
Since the list started empty you can only see the first item but you can scroll down to see the other items in the list:
The code in the clicks window is shown below. Display_sum is a helper function written by the programmer that updates the sum_label with the sum of the numbers in the parameter. It uses the get_items method which returns a list of the items (numbers) in the list. Integer_list_select tells the project to delete a number when the user clicks on it, allowing the user to remove numbers from the list. New_number_return tells the project to add a number when the user types it into the entry and hits enter.
def
display_sum(the_list):
''' displays the sum of the numbers in a list widget of numbers
'''
sum = 0
# tell user if list is empty
if len(the_list.get_items()) == 0:
sum_label.set_text('Sum: Empty')
else:
for number in the_list.get_items():
sum += number
sum_label.set_text('Sum: '+str(sum))
def
integer_list_select():
''' delete the selected number '''
integer_list.delete(integer_list.selected())
display_sum(integer_list)
def new_number_return():
''' put the new number in the list '''
integer_list.add(int(new_number.get_text()))
# clear the number
new_number.set_text('')
# display the sum
display_sum(integer_list)
Below is the result when the user clicked on the 5 in the list. Since only the 7 remained the sum is now 7.
Contents