-
Notifications
You must be signed in to change notification settings - Fork 4
/
Gui_HA.py
291 lines (242 loc) · 11.4 KB
/
Gui_HA.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import Tkinter as Tk
import tkMessageBox
import tkFileDialog
import assignmentProb as CLI
from cStringIO import StringIO
import sys
class Capturing(list):
"""Class to capture output from a function"""
def __enter__(self):
self._stdout = sys.stdout
sys.stdout = self._stringio = StringIO()
return self
def __exit__(self, *args):
self.extend(self._stringio.getvalue().splitlines())
del self._stringio # free up some memory
sys.stdout = self._stdout
class Hungarian:
"""The main class dealing with the GUI input / output"""
def __init__(self, master):
self.r, self.c, self.Matrix = 0, 0, None
self.root = master
self.root.minsize(width=550, height=300)
self.root.title('HUNGARIAN ASSIGNMENT')
# topframe has a Label
self.topFrame = Tk.Frame(self.root)
self.topFrame.pack()
self.enterRC = Tk.Label(
self.topFrame, text="CONFIGURE ASSIGNMENT MODEl", fg="blue", bd=30,
font=("Arial", 20))
self.enterRC.pack(side=Tk.TOP)
# middle frame has two label and entry boxes
self.midFrame = Tk.Frame(self.root)
self.midFrame.pack()
textRow = Tk.Label(self.midFrame, text="Enter rows:", relief=Tk.SUNKEN,
bd=8, pady=4, padx=4, font=("Times New Roman", 16))
textCol = Tk.Label(self.midFrame, text="Enter cols:", relief=Tk.SUNKEN,
bd=8, pady=4, padx=4, font=("Times New Roman", 16))
self.rowEntry = Tk.Entry(self.midFrame, justify=Tk.CENTER, width=12,
font=("Times New Roman", 16))
self.colEntry = Tk.Entry(self.midFrame, justify=Tk.CENTER, width=12,
font=("Times New Roman", 16))
textRow.grid(row=0, pady=10)
textCol.grid(row=1, pady=10)
self.rowEntry.grid(row=0, column=1, padx=50)
self.colEntry.grid(row=1, column=1, padx=50)
# bottomFrame has necessary Buttons
self.bottomFrame = Tk.Frame(self.root, pady=50)
self.bottomFrame.pack(side=Tk.TOP)
self.quitBut = Tk.Button(self.bottomFrame, text="QUIT",
font=("Times New Roman", 16), fg="dark green",
command=self.topFrame.quit)
self.quitBut.pack(side=Tk.LEFT, padx=50)
self.nextBut = Tk.Button(self.bottomFrame, text="CONTINUE",
font=("Times New Roman", 16), fg="dark green",
command=self.validateInput)
self.nextBut.pack(side=Tk.RIGHT, padx=60)
def validateInput(self):
"""Verify whether the entered row/col are correct"""
r, c = self.rowEntry.get(), self.colEntry.get()
if not (r.isdigit() and c.isdigit()):
tkMessageBox.showinfo("Error", 'Enter valid rows and columns')
else:
self.r, self.c = int(r), int(c)
self.getAssignments()
def getAssignments(self):
"""Get the assignments from the user"""
for child in self.root.winfo_children():
child.pack_forget()
self.root.title('ENTER VALUES')
self.root.minsize(width=550, height=400)
# topframe has the fill label
topFrame = Tk.Frame(self.root)
topFrame.pack()
self.fillRC = Tk.Label(topFrame, text="Fill %d X %d matrix" % (
self.r, self.c), fg="blue", bd=20, font=("Times New Roman", 20))
self.fillRC.pack(side=Tk.TOP)
# midframe allows the user to enter assignments
midFrame = Tk.Frame(self.root)
midFrame.pack()
self.Mat_entry = [[0 for i in xrange(self.c)] for j in xrange(self.r)]
self.Matrix = [[0 for i in xrange(self.c)] for j in xrange(self.r)]
for i in xrange(self.r):
for j in xrange(self.c):
self.Mat_entry[i][j] = Tk.Entry(midFrame, justify=Tk.CENTER,
fg="orange", bg="black",
width=6, font=("", 16))
self.Mat_entry[i][j].insert(0, "0")
for i in xrange(self.r):
for j in xrange(self.c):
self.Mat_entry[i][j].grid(row=i, column=j, padx=10, pady=10)
# bottomFrame has the necessary Buttons
bottomFrame = Tk.Frame(self.root, pady=50)
bottomFrame.pack(side=Tk.TOP)
self.resetBut = Tk.Button(bottomFrame, text="RESET", bg="#1f0b3b",
fg="cyan", font=("Times New Roman", 16),
command=self.resetAssignments)
self.backBut = Tk.Button(bottomFrame, text="BACK", bg="#1f0b3b",
fg="cyan", font=("Times New Roman", 16),
command=self.restoreWindow)
self.solveBut = Tk.Button(bottomFrame, text="SOLVE", bg="#1f0b3b",
command=self.solveModel, fg="cyan",
font=("Times New Roman", 16))
self.resetBut.pack(side=Tk.LEFT, padx=50, pady=50)
self.backBut.pack(side=Tk.LEFT, padx=50)
self.solveBut.pack(side=Tk.RIGHT, padx=60)
def restoreWindow(self):
"""restores the user assignment window when back is pressed"""
if hasattr(self, "resultWindow"):
self.resultWindow.destroy()
for child in self.root.winfo_children():
child.destroy()
self.__init__(self.root)
else:
for child in self.root.winfo_children():
child.pack_forget()
self.root.minsize(width=550, height=300)
self.root.title('HUNGARIAN ASSIGNMENT')
self.topFrame.pack()
self.midFrame.pack()
self.bottomFrame.pack()
def resetAssignments(self):
"""reset user assignments to zero"""
for i in xrange(self.r):
for j in xrange(self.c):
self.Mat_entry[i][j].delete(0, Tk.END)
self.Mat_entry[i][j].insert(0, '0')
def solveModel(self):
"""Main stub that solves the MODEL
Uses CLI module which has the necessary functions
Store the results as well as the final result
"""
allIntegersEntered = True
for i in xrange(self.r):
for j in xrange(self.c):
if not (self.Mat_entry[i][j].get()).isdigit():
allIntegersEntered = False
break
else:
self.Matrix[i][j] = int(self.Mat_entry[i][j].get())
if not allIntegersEntered:
break
if not allIntegersEntered:
tkMessageBox.showinfo("Error", 'Only integer values are allowed')
self.resetAssignments()
else:
CLI._GUI_ROW = self.r
CLI._GUI_COL = self.c
CLI._GUI_M = self.Matrix
# Capture output from the module
with Capturing() as self.stepResults:
CLI.main()
self.resultOptionWindow()
self.finalResult = CLI.finalResult
self.stepResults = '\n'.join(self.stepResults)
def resultOptionWindow(self, created=False):
"""User can either view final result or the entire steps"""
if not created:
self.resultWindow = Tk.Toplevel(self.root)
self.resultWindow.resizable(False, False)
self.resultWindow.minsize(width=550, height=400)
self.resultWindow.title('RESULTS VIEWER')
optMode = Tk.Label(
self.resultWindow, text="Specify choice for the result",
fg="dark red", bd=30, font=("Times New Roman", 20))
optMode.pack()
modeABut = Tk.Button(self.resultWindow, text="View Final Result",
fg="white", command=self.viewFinalResult,
bg="#6e054a", font=("Times New Roman", 16))
modeBBut = Tk.Button(self.resultWindow, text="View steps", fg="white",
bg="#6e054a", command=self.viewIterationResult,
font=("Times New Roman", 16))
backBut = Tk.Button(self.resultWindow, text="Modify / Back",
bg="#6e054a", command=self.resultWindow.destroy,
fg="white", font=("Times New Roman", 16))
saveBut = Tk.Button(self.resultWindow, text="Save final", fg="white",
bg="#6e054a", command=self.saveResult,
font=("Times New Roman", 16))
modeABut.pack(padx=10, pady=20)
modeBBut.pack(padx=10, pady=30)
saveBut.pack(padx=10, pady=30)
backBut.pack(padx=10, pady=30)
def saveResult(self):
"""Saves the result on a file specified by the user"""
f = tkFileDialog.asksaveasfile(mode='w', defaultextension=".txt")
if f is None:
# asksaveasfile return `None` if dialog closed with "cancel".
return
# starts from `1.0`, not `0.0`
text2save = '\n'.join(self.stepResults.split(CLI.delimiter))
f.write(text2save)
f.close()
def goBack(self, param):
""" if param is false, simply clear current window
else go back to some other window after clearing"""
for child in self.resultWindow.winfo_children():
child.destroy()
if param:
self.resultOptionWindow(True)
def viewFinalResult(self):
"""View the final result of the assignment problem in a window"""
self.goBack(False)
self.resultWindow.minsize(width=450, height=400)
self.resultWindow.title('Final Results')
resVar = Tk.Label(
self.resultWindow, text=self.finalResult, fg="blue", bg='white',
font=("Times New Roman", 16))
backBut = Tk.Button(self.resultWindow, text="BACK", bg='black',
fg="orange", font=("Times New Roman", 16),
command=lambda: self.goBack(True))
resVar.pack(pady=30)
backBut.pack(padx=10, pady=20)
def viewIterationResult(self):
"""View the final result of the assignment problem in a Window"""
def showNextStep(resVar, generator):
try:
curText = generator.next()
resVar.config(text=curText)
except StopIteration:
resVar.config(text="\n Finished displaying result", fg='red',
bg='white', font=("Times New Roman", 16))
self.goBack(True)
self.goBack(False)
self.resultWindow.minsize(width=450, height=400)
self.resultWindow.title('Stepped Results')
gen = iter(self.stepResults.split(CLI.delimiter))
resVar = Tk.Label(
self.resultWindow, text="\n Press Next to view steps\n\n",
fg="blue", bg='white', font=("Times New Roman", 16))
resVar.pack(pady=30)
nextBut = Tk.Button(self.resultWindow, text="NEXT STEP", bg='black',
fg="orange", font=("Times New Roman", 16),
command=lambda: showNextStep(resVar, gen))
backBut = Tk.Button(self.resultWindow, text="BACK", bg='black',
fg="orange", font=("Times New Roman", 16),
command=lambda: self.goBack(True))
nextBut.pack(side=Tk.LEFT, padx=60, pady=20)
backBut.pack(side=Tk.LEFT, padx=60, pady=20)
if __name__ == '__main__':
root = Tk.Tk()
hGui = Hungarian(root)
root.mainloop()
pass