Skip to content

Instantly share code, notes, and snippets.

@Omustardo
Last active May 22, 2025 00:36
Show Gist options
  • Select an option

  • Save Omustardo/3243ef800da00e8814d809235279e784 to your computer and use it in GitHub Desktop.

Select an option

Save Omustardo/3243ef800da00e8814d809235279e784 to your computer and use it in GitHub Desktop.
Simple Python GUI "from scratch"
import tkinter as tk
# This program demonstrates how an application with a GUI might be created at its core.
# It creates a button by drawing a rectangle and handling mouse movement and clicks.
# It depends on tkinter (https://docs.python.org/3/library/tkinter.html) for creating a window, a drawable canvas, and getting mouse interactions.
# I generated this demo using Claude: https://claude.ai/share/62cd9df4-1ed7-40b7-a8f3-fb7e03e0146f
# Function to create and run our GUI application
def run_rectangle_demo():
# Create the main window
root = tk.Tk()
root.title("Simple Rectangle GUI Demo")
# Create a canvas widget
canvas = tk.Canvas(root, width=400, height=300, bg="white")
# Put a bit of spacing between the canvas and the rest of the content in the window.
canvas.pack(padx=10, pady=10)
# Rectangle properties
rect_x1, rect_y1 = 100, 100
rect_x2, rect_y2 = 300, 200
default_color = "blue"
hover_color = "red"
# Create a rectangle on the canvas
rect_id = canvas.create_rectangle(
rect_x1, rect_y1, rect_x2, rect_y2,
fill=default_color, outline="black", width=2
)
# Status text to show interactions
status_label = tk.Label(root, text="Move mouse over rectangle or click it")
status_label.pack(pady=5)
# Mouse state variable
mouse_over_rect = False
# Define event handler functions
def handle_click(event):
nonlocal mouse_over_rect
# Check if click is inside rectangle
if rect_x1 <= event.x <= rect_x2 and rect_y1 <= event.y <= rect_y2:
status_label.config(text="Rectangle clicked!")
print("Function executed: Rectangle was clicked!")
def handle_move(event):
nonlocal mouse_over_rect
# Check if mouse is inside rectangle
is_inside = rect_x1 <= event.x <= rect_x2 and rect_y1 <= event.y <= rect_y2
# Mouse entering rectangle
if is_inside and not mouse_over_rect:
canvas.itemconfig(rect_id, fill=hover_color)
status_label.config(text="Mouse over rectangle")
mouse_over_rect = True
# Mouse leaving rectangle
elif not is_inside and mouse_over_rect:
canvas.itemconfig(rect_id, fill=default_color)
status_label.config(text="Move mouse over rectangle or click it")
mouse_over_rect = False
# Bind events to the canvas
canvas.bind("<Button-1>", handle_click)
canvas.bind("<Motion>", handle_move)
# Start the main event loop
root.mainloop()
# Run the program
if __name__ == "__main__":
run_rectangle_demo()
@Omustardo
Copy link
Author

Omustardo commented May 21, 2025

Sorry for the lack of mouse pointer in the screenshots. In the first, I'm doing nothing. In the second, the mouse is over the button, and in the third the button was clicked.

Screenshot from 2025-05-21 12-22-16
Screenshot from 2025-05-21 12-22-18
Screenshot from 2025-05-21 12-22-19

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment