1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
import tkinter as tk class SimplePaint: def __init__(self, root): self.root = root self.root.title("Paint Simple") self.root.geometry("800x600") # Crear un Canvas para el área de dibujo self.canvas = tk.Canvas(root, bg="white") self.canvas.pack(fill=tk.BOTH, expand=True) # Crear un botón para borrar el dibujo self.clear_button = tk.Button(root, text="Borrar Dibujo", command=self.clear_canvas) self.clear_button.pack(side=tk.BOTTOM, fill=tk.X) # Variables para el dibujo self.drawing = False self.previous_x = 0 self.previous_y = 0 # Configurar eventos del ratón self.canvas.bind("<Button-1>", self.on_button_press) self.canvas.bind("<B1-Motion>", self.on_mouse_drag) def on_button_press(self, event): """Iniciar el dibujo cuando el botón del ratón se presiona""" self.drawing = True self.previous_x = event.x self.previous_y = event.y def on_mouse_drag(self, event): """Dibujar en el Canvas mientras el ratón se mueve""" if self.drawing: self.canvas.create_line(self.previous_x, self.previous_y, event.x, event.y, fill="black", width=2) self.previous_x = event.x self.previous_y = event.y def clear_canvas(self): """Limpiar el área de dibujo""" self.canvas.delete("all") # Crear la ventana principal root = tk.Tk() app = SimplePaint(root) root.mainloop() |