The Menu class subclasses the Rect class from CMU Graphics.
This means that normal Rect arguments like left and top
are still passed.
Keyword Arguments
The keyword arguments seen in the definition (aside from debug) are the default styles for Menu objects.
parent
A Menu object.
debug
When True, lines are drawn that show alignment and where the border is. This is useful if
fill and border are invisible, or you need guidelines to help align elements.
This also automatically calls the getData() method.
Attributes
Since Menu subclasses Rect, it has all the attributes a regular Rect.
MENUS, BUTTONS, TITLES
These are class level attributes that hold the data attribute of all instances of that class.
For insatnce, every buttons data attribute will be contained Menu.BUTTONS. They are all
of type List, so they can be accessed with bracket notation.
parent
The Rect of the parent. This will be None if no parent is passed.
buttons
A list of all the buttons that are children of this menu.
buttonsData
A list of all of the buttons data attribute.
debug
Whether or not debug mode is enabled.
dbNS
The line that runs from the center top to the center bottom of the button.
dbEW
The line that runs from the center left to the center bottom of the button.
dbNWSE
The line that runs from the top left corner to the bottom right corner of the button.
dbNESW
The line that runs from the top right corder to the bottom left corner of the button.
dbBorder
The border of the button.
data
A dictionary containing all the data of the button.
When the button is clicked, its onclick property is called.
Differences from Menu.Button.addEventListener
When this version of the event listener is called, it loops through all of the child buttons of the menu and checks to see if the
clicks coordinates collides with the button.
Buttons are automatically aligned to the center of their parent Menu.
Arguments
parent
A Menu object.
*args
These are treated mostly the same as they would be if this were a regualr Rect,
aside from left and top.
Left: Places the button in relation to the value of its parent menus
centerX value, instead of placing it in relation to the app window.
Negative values move the button left, while positive values move the button right.
Top: Places the button in relation to the value of its parent menus
top value, instead of placing it in relation to the app window.
Keyword Arguments
textValue
The text displayed on the button.
textFill
The color of the text.
textFont
The font of the text.
textSize
The size of the text.
textIsBold
Whether or not the text should be bold.
textIsItalic
Whether or not the text should be italic.
textOpacity
The opacity of the text.
textIsVisible
Whether or not the text should be visible.
onclick
The name of a function. Called when the parent menu has an event listener. Passing the name of the function with
parenthesis will call the function (foo() instead of foo).
debug
When True, lines are drawn that show alignment and where the border is. This is useful if
fill and border are invisible, or you need guidelines to help align elements.
This also automatically calls the getData() method.
Attributes
Since Menu.Button subclasses Rect, it
has all of the attributes a normal Rect would normally have.
parent
The parent Menu of the button.
onclick
The function the button is assigned to.
hasEventListener
Whether the button has an event listener or not.
text
A Label object. This is the text that is displayed on the button.
textValue
The value of the text that is displayed on the button.
textFill
The color of the text.
textSize
The size of the text.
textFont
The font of the text.
textIsBold
Whether or not the text is bold.
textIsItalic
Whether or not the text is italic.
textIsVisible
Whether or not the text is visible.
textOpacity
The opacity of the text.
debug
Whether or not debug mode is enabled.
dbNS
The line that runs from the center top to the center bottom of the button.
dbEW
The line that runs from the center left to the center bottom of the button.
dbNWSE
The line that runs from the top left corner to the bottom right corner of the button.
dbNESW
The line that runs from the top right corder to the bottom left corner of the button.
dbBorder
The border of the button.
data
A dictionary containing all the data of the button.
Event listeners are defined inside of the onMousePress function from CMU Graphics.
When an event listener is defined, they must take x and y as arguments. An example is shown below.
Either a Menu or Menu.Button object. This objects
fill and border properties must be rgb values. Passing
an object with a gradient or string color value will raise an error.
mode
Whether to darken or lighten the object on hover. Possible values are "darken"
and "lighten".
darkenModAmount
How much darker to make the object on hover.
lighenModAmount
How much lighter to make the object on hover.
hoverFill
Whether or not the background color should be effected.
hoverBorder
Whether or not the border color should be effected.
hoverText
Whether or not the text color should be effected.
Implementing Hover
Example
In its current state, Functions.hover requires a lot of boilerplate code
to work properly. This function only modifies the fill and border properties
of the button.
Functions.hover uses an app variable called hovering.
When the button is hovered over, the hovering varaible is increased, telling
the function that something is being hovered. Then, the function makes the fill and border
ligher. In the else block, we set the modified properties back to their
original state and tell the app that the button is no longer being hovered.
Colors
Colors are accessed like this: Colors.COLOR_NAME. So, to access the
gray color, you would do Colors.gray.
Name
Value
gray
rgb(200, 200, 200)
darkGray
rgb(175, 175, 175)
darkerGray
rgb(100, 100, 100)
Colors.CSS3
This class defines the colors available in CSS3. They are the same as the colors defined by CMU Graphics, just accessed in a
different way. To access them, do Colors.CSS3.COLOR_NAME. So, to access the color "cornflower blue", you would do Colors.CSS3.cornflowerblue.
Note that COLOR_NAME is all lower case.
These colors define the colors of the lines shown when the debug kwarg = True. While while it is possible to use and modify these colors, it is not recommended.