-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
211 lines (178 loc) · 5.91 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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import threading
import tkinter as tk
from tkinter import ttk
from src import sortRender
from src import dsRender
optionDict = {
"testStructure":13,
"Bubble Sort":1,
"Insertion Sort":2,
"Selection Sort":3,
"Merge Sort":4,
"Quick Sort":5,
"Stack":6,
"Queue":7,
"Linked List":8,
"Binary Tree":9,
"Binary Search Tree":10,
"AVL Tree":11,
"Graph":12,
}
class DataStructureVisualizer:
def __init__(self):
self.root = tk.Tk()
self.root.title("Data Structure Visualizer")
self.root.geometry("700x500")
self.root.configure(background="white")
self.root.resizable(False, False)
# when speed[1] = -1 : stop , 0 : pause , 1 : normal
self.speed = [50,-1]
def start_compute(self, SR , DR , selection):
print("start compute")
if(self.speed[1] == 0):
self.speed[1] = 1
return False
else:
self.speed[1] = 1
try:
option = optionDict.get(selection.get())
SR.startSort(selection) if option < 5 else DR.startGrid(selection)
self.speed[1] = -1
except Exception as e:
print(e)
if(e.args[0] == "Sort Stopped"):
SR.reset()
return True
def pause_compute(self):
self.speed[1] = 0
def stop_compute(self):
self.speed[1] = -1
def secondary_frame_setup(self):
secondary_frame = tk.Frame(self.root)
# title Frame
title_frame = tk.Frame(secondary_frame)
title_frame.pack()
title_label = tk.Label(
title_frame,
text="DSA Visualizer",
width=700,
font=("Serif Sans", 15),
background="black",
foreground="white",
justify="center",
anchor="center",
)
title_label.config(padx=10, pady=10)
title_label.pack()
# setting up the canvas
canvas_frame = tk.Frame(secondary_frame)
canvas_frame.pack(side=tk.LEFT)
canvas = tk.Canvas(canvas_frame, width=500, height=500, background="black")
canvas.pack(pady=10)
selection_frame = tk.Frame(secondary_frame)
selection_frame.configure(height=500, width=200)
selection_frame.pack(side=tk.RIGHT, anchor="n")
selection_label = tk.Label(
selection_frame,
text="Select Algorithm",
font=("Serif Sans", 10),
width="200",
background="black",
foreground="white",
)
selection_label.pack(pady=10)
selection = tk.ttk.Combobox(selection_frame, width=200)
selection["values"] = list(optionDict.keys())
selection.current(0)
selection.pack(pady=5)
SR = sortRender.sortRender(canvas, self.speed)
DR = dsRender.dsRender(canvas, self.speed)
flow_control_frame = tk.Frame(selection_frame, width=200)
flow_control_frame.pack(pady=10)
def start_button_clicked():
start_button.configure(text="Pause", command=pause_button_clicked)
completed = self.start_compute(SR , DR, selection)
if completed:
start_button.configure(text="Start", command=start_button_clicked)
def pause_button_clicked():
start_button.configure(text="Start", command=start_button_clicked)
self.pause_compute()
def reset_button_clicked():
self.stop_compute()
start_button.configure(text="Start", command=start_button_clicked)
SR.reset()
# start button
start_button = tk.Button(
flow_control_frame,
text="Start",
command=lambda: start_button_clicked(),
width=200,
)
start_button.pack(pady=10)
# reset button
reset_button = tk.Button(
flow_control_frame,
text="Reset",
command= reset_button_clicked,
width=200,
)
reset_button.pack(pady=10)
# frame for algo speed
speed_frame = tk.Frame(selection_frame, width=200)
speed_frame.pack(pady=10)
speed_label = tk.Label(
speed_frame,
text="Speed",
width=100,
background="black",
foreground="white",
)
speed_label.pack(pady=10)
speed_var = tk.IntVar(speed_frame, 5)
# scroll bar for speed
speed_scale = tk.Scale(
speed_frame,
from_=1,
to=100,
orient=tk.HORIZONTAL,
variable=speed_var,
resolution=1,
)
speed_scale.set(self.speed[0])
speed_scale.pack(pady=10)
# text box for speed
speed_entry = tk.Entry(
speed_frame,
textvariable=speed_var,
width=10,
)
speed_entry.insert(0, speed_var.get())
speed_entry.pack(side=tk.LEFT, pady=10)
def update_speed_scale(*args):
if speed_var.get():
self.speed[0] = min(100, speed_var.get())
else:
self.speed[0] = 1
speed_var.trace_add("write", update_speed_scale)
secondary_frame.pack()
# setting up the input box for data structures
input_frame = tk.Frame(selection_frame)
input_frame.pack(pady=10)
input_label = tk.Label(
input_frame,
text="Input",
width=200,
background="black",
foreground="white",
)
# if the user selects a data structure, then the input box will be displayed
def selection_changed(*args):
pass
def main_frame_setup(self):
pass
def run(self):
self.secondary_frame_setup()
self.root.mainloop()
if __name__ == "__main__":
visualizer = DataStructureVisualizer()
visualizer.run()