import tkinter as tk
from TkContext import TkContext
from contexts import async
from HardWork import *
class Application(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master, width=600, height=400)
self.grid(sticky=tk.N+tk.S+tk.W+tk.E)
self.grid_propagate(0)
self.hi1 = tk.Button(self)
self.hi1["text"] = "Count Words (sync)"
self.hi1["command"] = self.synchronous_count
self.hi1.grid(column=0, row=0, sticky=tk.E+tk.W)
self.hi2 = tk.Button(self)
self.hi2["text"] = "Count Words (async)"
self.hi2["command"] = self.asynchronous_count
self.hi2.grid(column=0, row=1, sticky=tk.E+tk.W)
self.QUIT = tk.Button(self, text="QUIT", fg="red", command=root.destroy)
self.QUIT.grid(column=0, row=2)
header = tk.Label(self)
header["text"] = "Status"
header.grid(column=1, row=0, sticky=tk.W)
self.status = []
for i in range(4):
self.status.append(tk.Label(self))
self.status[i].grid(column=1, row=1+i, sticky=tk.W)
@async
def asynchronous_count(self):
for label in self.status:
label["text"] = ""
words = yield load_words_async("Holmes.txt")
print("{0} words loaded".format(len(words)))
self.status[0]["text"] = "{0} words loaded".format(len(words))
self.status[1]["text"] = "Cleaning..."
words = yield clean_words_async(words)
print("{0} remain after cleaning".format(len(words)))
self.status[1]["text"] = "{0} remain after cleaning".format(len(words))
self.status[2]["text"] = "Counting..."
count = yield count_words_async(words)
print("{0} distinct words after counting".format(len(count)))
self.status[2]["text"] = "{0} distinct words after counting".format(len(count))
self.status[3]["text"] = "Sorting..."
most_common = yield get_most_common_async(count, 10)
print("The ten most common words: {0}".format(', '.join(most_common)))
self.status[3]["text"] = "The ten most common words: {0}".format(', '.join(most_common))
def synchronous_count(self):
for label in self.status:
label["text"] = ""
words = load_words("Holmes.txt")
print("{0} words loaded".format(len(words)))
self.status[0]["text"] = "{0} words loaded".format(len(words))
self.status[1]["text"] = "Cleaning..."
words = clean_words(words)
print("{0} remain after cleaning".format(len(words)))
self.status[1]["text"] = "{0} remain after cleaning".format(len(words))
self.status[2]["text"] = "Counting..."
count = count_words(words)
print("{0} distinct words after counting".format(len(count)))
self.status[2]["text"] = "{0} distinct words after counting".format(len(count))
self.status[3]["text"] = "Sorting..."
most_common = get_most_common(count, 10)
print("The ten most common words: {0}".format(', '.join(most_common)))
self.status[3]["text"] = "The ten most common words: {0}".format(', '.join(most_common))
root = tk.Tk()
app = Application(master=root)
with TkContext(app):
app.mainloop()