-
Notifications
You must be signed in to change notification settings - Fork 0
/
whiteboard.py
68 lines (51 loc) · 2.02 KB
/
whiteboard.py
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import tkinter as tk
from tkinter.colorchooser import askcolor
def start_drawing(event):
global is_drawing, prev_x, prev_y
is_drawing = True
prev_x, prev_y = event.x, event.y
def draw(event):
global is_drawing, prev_x, prev_y
if is_drawing:
current_x, current_y = event.x, event.y
canvas.create_line(prev_x, prev_y, current_x, current_y, fill=drawing_color, width=line_width, capstyle=tk.ROUND, smooth=True)
prev_x, prev_y = current_x, current_y
def stop_drawing(event):
global is_drawing
is_drawing = False
def change_pen_color():
global drawing_color
color = askcolor()[1]
if color:
drawing_color = color
def change_line_width(value):
global line_width
line_width = int(value)
root = tk.Tk()
root.title("Whiteboard App")
canvas = tk.Canvas(root, bg="white")
canvas.pack(fill="both", expand=True)
is_drawing = False
drawing_color = "black"
line_width = 2
root.geometry("800x600")
# Create a frame to hold the controls in the same line
controls_frame = tk.Frame(root)
controls_frame.pack(side="top", fill="x")
color_button = tk.Button(controls_frame, text="Change Color", command=change_pen_color)
clear_button = tk.Button(controls_frame, text="Clear Canvas", command=lambda: canvas.delete("all"))
color_button.pack(side="left", padx=5, pady=5)
clear_button.pack(side="left", padx=5, pady=5)
line_width_label = tk.Label(controls_frame, text="Line Width:")
line_width_label.pack(side="left", padx=5, pady=5)
line_width_slider = tk.Scale(controls_frame, from_=1, to=10, orient="horizontal", command=lambda val: change_line_width(val))
line_width_slider.set(line_width)
line_width_slider.pack(side="left", padx=5, pady=5)
canvas.bind("<Button-1>", start_drawing)
canvas.bind("<B1-Motion>", draw)
canvas.bind("<ButtonRelease-1>", stop_drawing)
text_widget_label = tk.Label(controls_frame, text="Notes:")
text_widget_label.pack(side="top", padx=5, pady=5)
text_widget = tk.Text(controls_frame, height=6, width=120)
text_widget.pack(side="left", padx=5, pady=5)
root.mainloop()