Looking to add a modern, interactive touch to your Python GUI projects? In this tutorial, you’ll learn how to create a sidebar menu that expands and collapses smoothly when clicked. Using Python Turtle, we’ll implement an animation that provides a fun and engaging user experience. This project is perfect for beginners looking to dive into GUI design and animations with Turtle graphics! 🚀✨
What You’ll Learn:
- How to create a sidebar menu using Python Turtle graphics 🖥️
- Implementing smooth expansion and collapse animations 🎬
- Handling mouse click events for user interaction 🖱️
- Creating and managing dynamic buttons inside the sidebar 🎭
- Designing a visually appealing UI with animations 🎨
Step 1: Set Up the Screen and Sidebar
We begin by setting up the Turtle screen with a black background and custom dimensions. Then, we create the sidebar turtle, which will be responsible for drawing and animating the sidebar.
import turtle
import time
# Set up screen
screen = turtle.Screen()
screen.bgcolor("black")
screen.setup(600, 500)
screen.tracer(0)
# Sidebar settings
sidebar = turtle.Turtle()
sidebar.speed(0)
sidebar.color("cyan")
sidebar.penup()
sidebar.goto(-300, 250)
sidebar.pendown()
Step 2: Define Sidebar State and Animation
We define the initial sidebar width and set it to expand and collapse smoothly. The sidebar’s expanded state will allow for a wider menu, and the collapsed state will display just a slim sidebar. The sidebar’s state is toggled when the user clicks on the screen.
# Sidebar state
sidebar_width = 50
expanded_width = 200
collapsed_width = 50
expanded = False
# Draw the sidebar
def draw_sidebar(width):
"""Draw the sidebar with a given width"""
sidebar.clear()
sidebar.penup()
sidebar.goto(-300, 250)
sidebar.pendown()
sidebar.begin_fill()
for _ in range(2):
sidebar.forward(width)
sidebar.right(90)
sidebar.forward(500)
sidebar.right(90)
sidebar.end_fill()
Step 3: Add Buttons Inside the Sidebar
Next, we create buttons inside the sidebar. These buttons will appear when the sidebar is expanded. The buttons have labels like Home, Settings, and Exit, and we will manage their creation and removal dynamically based on whether the sidebar is expanded or collapsed.
# Buttons inside the menu
buttons = []
def create_button(y_pos, label):
"""Create buttons inside the menu"""
button = turtle.Turtle()
button.speed(0)
button.color("white")
button.penup()
button.goto(-280, y_pos)
button.write(label, font=("Arial", 14, "bold"))
buttons.append(button)
Step 4: Implement the Toggle Function for Expanding and Collapsing the Sidebar
Now, we define the toggle_sidebar() function to handle the expansion and collapse of the sidebar. When the sidebar is expanded, we create buttons inside it. When collapsed, the buttons are cleared, and the sidebar shrinks back down.
def toggle_sidebar(x, y):
"""Expand or collapse the sidebar when clicked"""
global expanded
if expanded:
for w in range(expanded_width, collapsed_width, -10): # Collapse animation
draw_sidebar(w)
screen.update()
time.sleep(0.02)
clear_buttons()
else:
for w in range(collapsed_width, expanded_width, 10): # Expand animation
draw_sidebar(w)
screen.update()
time.sleep(0.02)
create_button(150, "Home")
create_button(100, "Settings")
create_button(50, "Exit")
expanded = not expanded # Toggle state
Step 5: Clear Buttons on Collapse
We define the clear_buttons() function to clear the buttons when the sidebar is collapsed.
def clear_buttons():
"""Clear buttons when collapsing the sidebar"""
for btn in buttons:
btn.clear()
buttons.clear()
Step 6: Trigger the Sidebar Animation with Mouse Click
Finally, we link the toggle_sidebar() function to a mouse click event. When the user clicks anywhere on the screen, the sidebar will either expand or collapse based on its current state.
# Draw initial sidebar
draw_sidebar(sidebar_width)
# Click event to toggle menu
screen.onclick(toggle_sidebar)
# Keep window open
turtle.done()
Conclusion
With this simple yet interactive sidebar menu, you’ve created a smooth expand/collapse animation using Python Turtle. This is a fantastic way to introduce GUI design and animations to your Python projects. By following this guide, you can easily build visually engaging menus for your own applications!
Watch the Full Tutorial
For a complete walkthrough, check out the YouTube tutorial below:
Code Repository
Access the complete source code on GitHub:
By following this tutorial, you’ll be able to create fully interactive Python applications with smooth animations and responsive user interfaces. If you enjoyed this tutorial, don’t forget to like, comment, and subscribe for more exciting Python tutorials! 🚀🎨