Creates a turtle-based button with a label.
(name, x, y, width=120, height=40)
| 44 | buttons = {} # Dictionary to hold button turtles and their properties |
| 45 | |
| 46 | def create_button(name, x, y, width=120, height=40): |
| 47 | """Creates a turtle-based button with a label.""" |
| 48 | if name in buttons and buttons[name]['turtle'] is not None: |
| 49 | buttons[name]['turtle'].clear() |
| 50 | |
| 51 | button_turtle = Turtle() |
| 52 | button_turtle.hideturtle() |
| 53 | button_turtle.penup() |
| 54 | button_turtle.speed("fastest") |
| 55 | |
| 56 | button_turtle.goto(x - width/2, y - height/2) |
| 57 | button_turtle.color(colors.BUTTON_BORDER_COLOR, colors.BUTTON_BG_COLOR) |
| 58 | button_turtle.begin_fill() |
| 59 | for _ in range(2): |
| 60 | button_turtle.forward(width) |
| 61 | button_turtle.left(90) |
| 62 | button_turtle.forward(height) |
| 63 | button_turtle.left(90) |
| 64 | button_turtle.end_fill() |
| 65 | |
| 66 | button_turtle.goto(x, y - 12) |
| 67 | button_turtle.color(colors.BUTTON_TEXT_COLOR) |
| 68 | button_turtle.write(name, align="center", font=("Lucida Sans", 14, "bold")) |
| 69 | |
| 70 | buttons[name] = {'turtle': button_turtle, 'x': x, 'y': y, 'w': width, 'h': height, 'visible': True} |
| 71 | |
| 72 | def hide_button(name): |
| 73 | """Hides a button by clearing its turtle.""" |