Toggle

Toggle objects are created using the following function. A toggle is specified by two diagonal corners, its beginning state, the function to call when the user presses it, its various colors (background, foreground, and outline), and outline thickness.

FunctionDescription
Toggle(x1, y1, x2, y2, startState, updateFunction, foreground, background, outline, thickness)Creates a toggle button.
  • x1 and y1 specify top-left corner, x2 and y2 specify the bottom-right corner.
  • startState is the initial value of the toggle as a boolean (default is False).
  • updateFunction specifies what to do when clicked.
  • foreground is the color of the toggle when clicked (default is Color.RED).
  • background is the color behind it (default is Color.BLACK).
  • outline specifies the color of the toggle’s outline (default is the same as foreground).
  • thickness specifies the thickness of the outline in pixels (default is 3).

You can create a toggle as follows:

from gui import *

d = Display()

# function to specify what happens when button is pressed
def printValue( value ):

   if value:   # if value is True, push toggle is on

      print "Yes"   # replace this with whatever you want done when on

   else:       # else value is False (i.e., push toggle is off)

      print "No"   # replace this with whatever you want done when off (if any)
 

t = Toggle(25, 25, 50, 50, False, printValue, Color.WHITE, Color.BLACK, Color.RED, 1)
d.add(t)

Once a toggle has been created, the following functions are available:

FunctionDescription
t.getValue()Returns the current value of the toggle (True or False).
t.setValue(value)Sets the current value of the toggle to value.