##
# Fill boxes with colour with mouse events
#
from random import choice
from guizero import App, Box, Window
app = App(layout="grid", width=200, height=200)
COLOURS = [
"snow", "gainsboro", "floral white", "old lace", "linen", "antique white",
"papaya whip", "blanched almond", "peach puff", "navajo white", "lemon chiffon",
"mint cream", "azure", "alice blue", "lavender", "lavender blush", "misty rose",
"dark slate gray", "slate gray", "light slate gray", "gray", "light grey", "midnight blue",
"navy", "cornflower blue", "slate blue", "medium slate blue", "light slate blue",
"medium blue", "royal blue", "blue", "dodger blue", "deep sky blue", "sky blue",
"light sky blue", "steel blue", "light steel blue", "light blue", "powder blue",
"pale turquoise", "dark turquoise", "medium turquoise", "turquoise", "cyan",
"light cyan", "cadet blue", "medium aquamarine", "aquamarine", "dark green",
"dark olive green", "dark sea green", "sea green", "medium sea green", "light sea green",
"pale green", "spring green", "lawn green", "medium spring green", "green yellow",
"lime green", "yellow green", "forest green", "olive drab", "dark khaki",
"khaki", "pale goldenrod", "light goldenrod yellow", "light yellow", "yellow",
"gold", "light goldenrod", "goldenrod", "dark goldenrod", "rosy brown", "indian red",
]
def fill_bg(event):
"""
Called when mouse enters box
Args:
event (guizero.event.EventData): Event object
"""
if str(event.widget.grid) == str([0, 0]):
event.widget.bg = choice(COLOURS)
elif str(event.widget.grid) == str([1, 0]):
event.widget.bg = choice(COLOURS)
elif str(event.widget.grid) == str([0, 1]):
event.widget.bg = "green"
elif str(event.widget.grid) == str([1, 1]):
event.widget.bg = "#ff00ff"
def fill_cleared(event):
"""
Called when mouse leaves box
Args:
event (guizero.event.EventData): Event object
"""
event.widget.bg = None
def mouse_position(event):
"""
Called when mouse clicked or dragged
Args:
event (guizero.event.EventData): Event object
"""
print(
f"mouse position absolute: {event.display_x}, {event.display_y}, mouse position relative: {event.x}, {event.y} ",
end="\r",
)
def new_window(event):
"""
Called when right mouse click
Args:
event (guizero.event.EventData): Event object
"""
window = Window(app, title="2nd Window", height=200, width=100)
width = 100
height = 100
boxes = []
count = 0
for x in [0, 1]:
for y in [0, 1]:
# Create a list of boxes
boxes.append(Box(app, width=width, height=height, grid=[x, y], border=1))
# Add events
boxes[count].when_mouse_enters = fill_bg
boxes[count].when_mouse_leaves = fill_cleared
boxes[count].when_clicked = mouse_position
# boxes[count].when_left_button_pressed = say_hello
boxes[count].when_right_button_pressed = new_window
boxes[count].when_mouse_dragged = mouse_position
count += 1
app.display()