Untitled
unknown
plain_text
a year ago
1.5 kB
2
Indexable
Never
from tkinter import * from tkinter.colorchooser import askcolor class DrawingApp: def __init__(self, master): self.master = master self.master.title("Drawing App") self.pen_size = 5 self.color = "black" self.old_coords = None self.canvas = Canvas(self.master, width=500, height=500, bg="white") self.canvas.pack(expand=YES, fill=BOTH) self.canvas.bind("<B1-Motion>", self.draw) self.pen_button = Button(self.master, text="Pen", command=self.use_pen) self.pen_button.pack(side=LEFT) self.color_button = Button(self.master, text="Color", command=self.choose_color) self.color_button.pack(side=LEFT) self.clear_button = Button(self.master, text="Clear", command=self.clear_canvas) self.clear_button.pack(side=LEFT) def draw(self, event): if self.old_coords: x1, y1 = self.old_coords x2, y2 = event.x, event.y self.canvas.create_line(x1, y1, x2, y2, width=self.pen_size, fill=self.color, capstyle=ROUND, smooth=TRUE) self.old_coords = event.x, event.y def use_pen(self): self.canvas.bind("<B1-Motion>", self.draw) def choose_color(self): self.color = askcolor()[1] def clear_canvas(self): self.canvas.delete("all") self.old_coords = None if __name__ == "__main__": root = Tk() app = DrawingApp(root) root.mainloop()