Skip to content

Instantly share code, notes, and snippets.

@v3c70r
Created August 31, 2015 18:45
Show Gist options
  • Save v3c70r/04b6bb5926f8691963b1 to your computer and use it in GitHub Desktop.
Save v3c70r/04b6bb5926f8691963b1 to your computer and use it in GitHub Desktop.
import Tkinter, Tkconstants, tkFileDialog
import DFS_BFS
class TkFileDialogExample(Tkinter.Frame):
adjPath = ''
infoPath = ''
res = []
def __init__(self, root):
self.stepIdx=0
Tkinter.Frame.__init__(self, root)
# define buttons
Tkinter.Button(self, text='Open Adj Matrix', command=self.openAdj).grid(row=0, column=0)
Tkinter.Button(self, text='Open Info Matrix', command=self.openInfo).grid(row=1, column=0)
Tkinter.Button(self, text='BFS', command=self.doBFS).grid(row=3, column=0, padx=5, pady=5)
Tkinter.Button(self, text='DFS', command=self.doDFS).grid(row=3, column=1, padx=5, pady=5)
#Step control
Tkinter.Button(self, text='Next Step', command=self.showNextStep).grid(row=5, column=0, padx=5, pady=5)
Tkinter.Button(self, text='Show Rest', command=self.showRest).grid(row=5, column=1, padx=5, pady=5)
Tkinter.Button(self, text='Clear', command=self.clean).grid(row=6, column=0, padx=5, pady=5)
Tkinter.Button(self, text='Write to File', command=self.writeFile).grid(row=6, column=1, padx=5, pady=5)
#define texts
self.adjPathText = Tkinter.Text(self, height=1, width=70)
self.adjPathText.grid(row=0, column=1);
self.adjPathText.config(highlightbackground="gray")
self.infoPathText = Tkinter.Text(self, height=1, width=70)
self.infoPathText.grid(row=1, column=1);
self.infoPathText.config(highlightbackground="gray")
self.infoBox = Tkinter.Text(self, height=40, width=86)
self.infoBox.grid(row=4, column=0, columnspan=2);
self.infoBox.config(highlightbackground="gray")
# define options for opening or saving a file
self.file_opt = options = {}
options['defaultextension'] = '.txt'
options['filetypes'] = [('all files', '.*'), ('text files', '.txt')]
options['initialfile'] = 'results.txt'
options['parent'] = root
options['title'] = 'This is a title'
options['initialdir'] = './DataAssignment_1/'
# This is only available on the Macintosh, and only when Navigation Services are installed.
#options['message'] = 'message'
# if you use the multiple file version of the module functions this option is set automatically.
#options['multiple'] = 1
# defining options for opening a directory
self.dir_opt = options = {}
options['initialdir'] = './'
options['mustexist'] = False
options['parent'] = root
options['title'] = 'This is a title'
def openAdj(self):
self.adjPath = tkFileDialog.askopenfilename(**self.file_opt)
self.adjPathText.delete("1.0",Tkinter.END);
self.adjPathText.insert(Tkinter.END, self.adjPath);
def openInfo(self):
self.infoPath = tkFileDialog.askopenfilename(**self.file_opt)
self.infoPathText.delete("1.0",Tkinter.END);
self.infoPathText.insert(Tkinter.END, self.infoPath);
def doBFS(self):
if self.adjPath == '' or self.infoPath == '':
self.infoBox.insert(Tkinter.END, "[ERROR] No adj file or info path loaded\n")
return;
#reset
self.infoBox.delete("1.0",Tkinter.END);
self.infoBox.insert(Tkinter.END, "Processing ...")
graph = DFS_BFS.GraphSearcher()
graph.init(self.adjPath, self.infoPath)
graph.BFS()
self.res = graph.getRes()
self.infoBox.insert(Tkinter.END, "DONE\n")
#self.infoBox.insert(Tkinter.END, "".join(graph.getRes()));
def doDFS(self):
if self.adjPath == '' or self.infoPath == '':
self.infoBox.insert(Tkinter.END, "[ERROR] No adj file or info path loaded\n")
return;
#reset
self.infoBox.delete("1.0",Tkinter.END);
self.infoBox.insert(Tkinter.END, "Processing ...")
graph = DFS_BFS.GraphSearcher()
graph.init(self.adjPath, self.infoPath)
graph.DFS()
self.res = graph.getRes()
self.infoBox.insert(Tkinter.END, "DONE\n")
def showNextStep(self):
if self.stepIdx < len(self.res):
processIdxStr = '['+str(self.stepIdx+1)+'/'+str(len(self.res))+'] '
self.infoBox.insert(Tkinter.END, processIdxStr+self.res[self.stepIdx])
self.infoBox.see(Tkinter.END)
self.stepIdx = self.stepIdx+1
def showRest(self):
for self.stepIdx in range(0, len(self.res)):
processIdxStr = '['+str(self.stepIdx+1)+'/'+str(len(self.res))+'] '
self.infoBox.insert(Tkinter.END, processIdxStr+self.res[self.stepIdx])
self.infoBox.see(Tkinter.END)
self.stepIdx=self.stepIdx+1;
def clean(self):
self.stepIdx = 0
self.infoBox.delete("1.0",Tkinter.END);
def writeFile(self):
writeFileName = tkFileDialog.asksaveasfilename(**self.file_opt)
f = open(writeFileName, 'w')
f.write("".join(self.res))
if __name__=='__main__':
root = Tkinter.Tk()
TkFileDialogExample(root).pack()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment