ouali Code :
code source : import tkinter as tk
from tkinter import colorchooser
class DrawingApp:
def __init__(self,root):
self.root=root
self.root.geometry('600x400')
self.canvas = tk.Canvas(root,bg='white'
,width=600,height=350)
self.canvas.pack()
self.button_frame=tk.Frame(root)
self.button_frame.pack(pady=10)
self.brush_color='black'
self.color_button=tk.Button(self.button_frame,
text='Change Color',command=self.change_color,
font=('Helvetica',15),bg='#0080ff')
self.color_button.pack(side=tk.LEFT,padx=5)
self.color_button=tk.Button(self.button_frame,
text='Clear Canvas',command=self.clear_canvas,
font=('Helvetica',15),bg='#ff0000')
self.color_button.pack(side=tk.LEFT,padx=5)
self.canvas.bind('',self.draw)
self.canvas.bind('',self.reset)
self.last_x=None
self.last_y=None
def draw(self,event):
"""Draw on the canvas"""
if self.last_x and self.last_y:
self.canvas.create_line(self.last_x
,self.last_y,event.x,event.y,width=3
,fill=self.brush_color,capstyle=tk.ROUND,
smooth=tk.TRUE)
self.last_x = event.x
self.last_y = event.y
def reset(self,event):
"""Reset the last drawing coordinates"""
self.last_x = None
self.last_y = None
def change_color(self):
"""Change the brush color using
a color chooser"""
color=colorchooser.askcolor(
title='Choose a color')
if color[1]:
self.brush_color = color[1]
def clear_canvas(self):
"""Clear the canvas"""
self.canvas.delete('all')
root = tk.Tk()
app=DrawingApp(root)
root.mainloop()
2025-02-22 11:07:10