-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
74 lines (56 loc) · 2.72 KB
/
main.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
69
70
71
72
73
74
import customtkinter as ctk
from draw_surface import DrawSurface
from tool_panel import ToolPanel
from settings import *
class App(ctk.CTk):
def __init__(self):
# Create window
super().__init__()
# Window settings
self.geometry('800x600')
self.title('Whiteboard')
self.iconbitmap(resource_path('paint_ico.ico'))
ctk.set_appearance_mode('light')
# Data
self.color_string = ctk.StringVar(value='000')
self.brush_float = ctk.DoubleVar(value=0.1) # 0.1 <-> 1
self.erase_bool = ctk.BooleanVar(value=False)
# Widgets
self.draw_surface = DrawSurface(self, self.color_string, self.brush_float, self.erase_bool)
self.tool_panel = ToolPanel(self, self.brush_float, self.color_string, self.draw_surface.undo, self.draw_surface.redo, self.draw_surface.save, self.draw_surface.open, self.clear_canvas, self.erase_bool)
# Undo/Redo events
self.bind('<Control-z>', self.draw_surface.undo)
self.bind('<Control-Z>', self.draw_surface.redo)
# Save events
self.bind('<Control-s>', lambda event: self.draw_surface.save(event, self.tool_panel))
# Open events
self.bind('<Control-o>', lambda event: self.draw_surface.open(event, self.tool_panel))
# Mousewheel event
self.bind('<MouseWheel>', self.adjust_brush_size)
# Color picker event
self.bind('<Tab>', self.color_pick)
# Init window
self.mainloop()
def color_pick(self, event):
# Find all drawn items at the mouse cursor on the canvas
items = self.draw_surface.find_overlapping(event.x, event.y, event.x, event.y)
# If items were found at the location
if items:
# Get the id of the topmost item
item_id = items[-1]
# Retrieve the color of the item
item_color = self.draw_surface.itemcget(item_id, 'fill')
# Set the color string to the color found, so long as its not the canvas color
if item_color != '#f5f6ff':
self.color_string.set(item_color[1:])
else:
print(f'No color at ({event.x}, {event.y})')
def adjust_brush_size(self, event):
new_brush_size = self.brush_float.get() + 0.05 * event.delta/120
new_brush_size = min(1, max(0.1, new_brush_size))
self.brush_float.set(new_brush_size)
def clear_canvas(self):
self.draw_surface.delete('all')
if __name__ == '__main__':
loadfont(resource_path('PixulBrush.ttf'))
App()