Toggle

A toggle can be drawn on a display and used to control the parameters being given to a particular function via whether the toggle is on or off, which is controlled by clicking with the mouse. As an example, a toggle could be used to control whether a sample is playing or off.

A toggle is specified by two diagonal corners, its beginning state, the function to call when the user interacts with it, its various colors (background, foreground, and outline), and outline thickness.

To create a toggle, you must have a display to draw it on.

d = Display()

When creating a new toggle object, you need to store it in a variable (or other memory location) to be able to use it later.

Function Description
Toggle(x1, y1, x2, y2, startState, updateFunction, foreground, background, outline, thickness) Creates a toggle.  x1 and y1 specify coordinates of one corner, x2 and y2 specify coordinates of the diagonal corner. startState is the initial value of the toggle (default is False). updateFunction (default is None) is the function that this toggle calls when clicked. foreground is the color of the toggle when clicked (default is red), background is the color behind it (default is black), outline specifies the color of the the toggle’s outline (default is to be the same color as the foreground), and thickness specifies the thickness in pixels of this outline (default is 3).

You can create a toggle as follows:

t = Toggle(25, 25, 50, 50, False, None, Color.WHITE, Color.BLACK, Color.RED, 1)

and add it to the display:

d.add(t)

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

Function Description
t.getValue() Returns the current value of the toggle (True or False).
t.setValue(newValue) Sets the current value of the toggle to ‘pushValue’.