-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui_handler.py
175 lines (142 loc) · 7.15 KB
/
gui_handler.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
from tkinter import Label, Canvas, Button, Tk, Listbox
class grid_editor_GUI:
def __init__(self, master_grid):
self.master = Tk()
self.master_grid = master_grid
self.master.title("Grid editor")
# Big title
self.label = Label(self.master, text="Artistic Representation of a Literary Criticism - Andrew Chen",
font=("Courier", 24))
self.label.grid(row=0, column=0, columnspan=9, pady=10)
background_color = 'white smoke'
# Canvas titles
self.labelXZ = Label(self.master, text="XZ View", font=("Courier", 14))
self.labelYZ = Label(self.master, text="YZ View", font=("Courier", 14))
self.labelXY = Label(self.master, text="XY View", font=("Courier", 14))
self.labelXZ.grid(row=1, column=0, columnspan=3)
self.labelYZ.grid(row=1, column=3, columnspan=3)
self.labelXY.grid(row=1, column=6, columnspan=3)
# Canvas XZ
self.canvas_XZ = Canvas(self.master, bg="white", height=360, width=360, background=background_color,
highlightthickness=1, highlightbackground="black")
self.canvas_XZ.grid(row=2, column=0, rowspan=3, columnspan=3, padx=20, pady=10)
self.set_canvas(self.canvas_XZ)
# Canvas YZ
self.canvas_YZ = Canvas(self.master, bg="white", height=360, width=360, background=background_color,
highlightthickness=1, highlightbackground="black")
self.canvas_YZ.grid(row=2, column=3, rowspan=3, columnspan=3, padx=20, pady=10)
self.set_canvas(self.canvas_YZ)
# Canvas XY
self.canvas_XY = Canvas(self.master, bg="white", height=360, width=360, background=background_color,
highlightthickness=1, highlightbackground="black")
self.canvas_XY.grid(row=2, column=6, rowspan=3, columnspan=3, padx=20, pady=10)
self.set_canvas(self.canvas_XY)
# Bind painting
self.canvas_XZ.bind("<B1-Motion>", self.paint(self.canvas_XZ))
self.canvas_YZ.bind("<B1-Motion>", self.paint(self.canvas_YZ))
self.canvas_XY.bind("<B1-Motion>", self.paint(self.canvas_XY))
# Reset buttons
self.reset_button_XZ = Button(self.master, text="Clear", command=self.reset_canvas(self.canvas_XZ))
self.reset_button_YZ = Button(self.master, text="Clear", command=self.reset_canvas(self.canvas_YZ))
self.reset_button_XY = Button(self.master, text="Clear", command=self.reset_canvas(self.canvas_XY))
self.reset_button_XZ.grid(row=5, column=0, ipadx=5, ipady=5)
self.reset_button_YZ.grid(row=5, column=3, ipadx=5, ipady=5)
self.reset_button_XY.grid(row=5, column=6, ipadx=5, ipady=5)
# TODO Fill buttons
self.fill_button_XZ = Button(self.master, text="Fill", command=self.fill_canvas(self.canvas_XZ))
self.fill_button_YZ = Button(self.master, text="Fill", command=self.fill_canvas(self.canvas_YZ))
self.fill_button_XY = Button(self.master, text="Fill", command=self.fill_canvas(self.canvas_XY))
self.fill_button_XZ.grid(row=5, column=2, ipadx=5, ipady=5)
self.fill_button_YZ.grid(row=5, column=5, ipadx=5, ipady=5)
self.fill_button_XY.grid(row=5, column=8, ipadx=5, ipady=5)
# Finish button
self.finish_button = Button(self.master, text="Save", command=self.finish) # TODO command to export grid
self.finish_button.grid(row=6, column=5, ipady=5)
# Pixelated view button
self.pixelate_button = Button(self.master, text="Pixelate", command=self.pixelate)
self.pixelate_button.grid(row=6, column=3, ipady=5)
self.master.mainloop()
def finish(self):
self.set_2dgrids()
self.master.destroy()
def set_2dgrids(self):
XZ_pixelated_matrix = self.get_pixelated_matrix(self.canvas_XZ)
YZ_pixelated_matrix = self.get_pixelated_matrix(self.canvas_YZ)
XY_pixelated_matrix = self.get_pixelated_matrix(self.canvas_XY)
for i in range(18):
for j in range(18):
self.master_grid.XZ[i + 1][j + 1] = XZ_pixelated_matrix[i][j]
self.master_grid.YZ[i + 1][j + 1] = YZ_pixelated_matrix[i][j]
self.master_grid.XY[i + 1][j + 1] = XY_pixelated_matrix[i][j]
def fill_canvas(self, canvas):
def foo():
canvas.create_rectangle(0, 0, 360, 360, fill='black')
return foo
def reset_canvas(self, canvas): # for button presses
def foo():
canvas.delete('all')
for i in range(18):
canvas.create_line(i * 20, 0, i * 20, 360)
canvas.create_line(0, i * 20, 360, i * 20)
return foo
def set_canvas(self, canvas): # for clearing the canvas
canvas.delete('all')
for i in range(18):
canvas.create_line(i * 20, 0, i * 20, 360)
canvas.create_line(0, i * 20, 360, i * 20)
def paint(self, canvas):
def ret(event):
radius = 10
x1, y1 = (event.x - radius), (event.y - radius)
x2, y2 = (event.x + radius), (event.y + radius)
canvas.create_oval(x1, y1, x2, y2, fill='black')
return ret
def pixelate(self):
self.pixelate_canvas(self.canvas_XZ)
self.pixelate_canvas(self.canvas_YZ)
self.pixelate_canvas(self.canvas_XY)
def pixelate_canvas(self, canvas):
pixelated_matrix = self.get_pixelated_matrix(canvas)
self.set_canvas(canvas)
for i in range(18):
for j in range(18):
if (pixelated_matrix[i][j]):
canvas.create_rectangle(i * 20, j * 20, i * 20 + 20, j * 20 + 20, fill='black')
def get_pixelated_matrix(self, canvas):
pixelated_matrix = [[False for i in range(18)] for j in range(18)]
for i in range(18):
for j in range(18):
if (self.pixel_is_black(canvas, i * 20 + 10, j * 20 + 10)):
pixelated_matrix[i][j] = True
return pixelated_matrix
def pixel_is_black(self, canvas, x, y):
ids = canvas.find_overlapping(x, y, x, y)
if len(ids) > 0:
index = ids[-1]
color = canvas.itemcget(index, "fill")
color = color.upper()
if color != '':
return True
return False
class grid_plotter_GUI:
def __init__(self, filenames, selection):
self.master = Tk()
self.selection = selection
self.master.title("Grid plotter")
# Big title
self.label = Label(self.master, text="Select the grid to plot:",
font=("Courier", 18))
self.label.pack()
# List box
self.listbox = Listbox(self.master, font=("Courier", 14))
index = 1
for filename in filenames:
self.listbox.insert(index, filename[:-5])
self.listbox.pack()
# Finish button
self.finish_button = Button(self.master, text="Display plot", command=self.finish)
self.finish_button.pack()
self.master.mainloop()
def finish(self):
self.selection[0] = self.listbox.get(self.listbox.curselection(), self.listbox.curselection())
self.master.destroy()