Lists in GUI4sher

List for Selecting a String

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:

Graphical user interface, text, application

Description automatically generated

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.

Text

Description automatically generated

This is what selecting looks like:

Graphical user interface, text, application

Description automatically generated

Graphical user interface, text, application

Description automatically generated

Graphical user interface, text, application

Description automatically generated

Editable List of Numbers

Widgets in your project can modify a list.  This is an example of a list of numbers:

Graphical user interface, text, application

Description automatically generated

When the user puts an integer in the entry (top middle):

Graphical user interface, text, application

Description automatically generated

And then when the user hits enter, the number is added to the list and the sum is updated:

Graphical user interface, text, application

Description automatically generated

Putting a 7 in the list makes the sum 12:

Graphical user interface, text, application

Description automatically generated

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:

Graphical user interface, text, application, Word

Description automatically generated

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.

Graphical user interface, text, application

Description automatically generated

 

Click to see project administration tools

Contents

·      GUI4sher How To

o   Downloading

o   Starting a new project

·      The Structure of a GUI4sher project

·      Adding Geometric Objects to a GUI4sher Project

o   Geometric Object

o   Rectangles

o   Modifying Colors and Borders

·      Ovals and Lines[SD1] 

o   Ovals

o   Lines

o   Polygons

·      Labels: Putting Words on GUI4sher

·      Buttons: Simple User Input

o   Simple Button

o   Counting Button

o   Check

·      Entering Text

o   Entries

o   Text

o   Creating an App

·      Lists in GUI4sher

o   List for Selecting a String

o   Editable List of Numbers

·       Project Administration

o   Copyright and Thanks

o   Debugging

 


 [SD1]