SHARE
TWEET

Python async tkapp

a guest Oct 15th, 2012 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import tkinter as tk
  2. from TkContext import TkContext
  3.  
  4. from contexts import async
  5. from HardWork import *
  6.  
  7. class Application(tk.Frame):
  8.     def __init__(self, master=None):
  9.         tk.Frame.__init__(self, master, width=600, height=400)
  10.  
  11.         self.grid(sticky=tk.N+tk.S+tk.W+tk.E)
  12.         self.grid_propagate(0)
  13.  
  14.         self.hi1 = tk.Button(self)
  15.         self.hi1["text"] = "Count Words (sync)"
  16.         self.hi1["command"] = self.synchronous_count
  17.         self.hi1.grid(column=0, row=0, sticky=tk.E+tk.W)
  18.  
  19.         self.hi2 = tk.Button(self)
  20.         self.hi2["text"] = "Count Words (async)"
  21.         self.hi2["command"] = self.asynchronous_count
  22.         self.hi2.grid(column=0, row=1, sticky=tk.E+tk.W)
  23.  
  24.         self.QUIT = tk.Button(self, text="QUIT", fg="red", command=root.destroy)
  25.         self.QUIT.grid(column=0, row=2)
  26.  
  27.         header = tk.Label(self)
  28.         header["text"] = "Status"
  29.         header.grid(column=1, row=0, sticky=tk.W)
  30.  
  31.         self.status = []
  32.         for i in range(4):
  33.             self.status.append(tk.Label(self))
  34.             self.status[i].grid(column=1, row=1+i, sticky=tk.W)
  35.  
  36.  
  37.     @async
  38.     def asynchronous_count(self):
  39.         for label in self.status:
  40.             label["text"] = ""
  41.  
  42.         words = yield load_words_async("Holmes.txt")
  43.         print("{0} words loaded".format(len(words)))
  44.         self.status[0]["text"] = "{0} words loaded".format(len(words))
  45.  
  46.         self.status[1]["text"] = "Cleaning..."
  47.         words = yield clean_words_async(words)
  48.         print("{0} remain after cleaning".format(len(words)))
  49.         self.status[1]["text"] = "{0} remain after cleaning".format(len(words))
  50.  
  51.         self.status[2]["text"] = "Counting..."
  52.         count = yield count_words_async(words)
  53.         print("{0} distinct words after counting".format(len(count)))
  54.         self.status[2]["text"] = "{0} distinct words after counting".format(len(count))
  55.  
  56.         self.status[3]["text"] = "Sorting..."
  57.         most_common = yield get_most_common_async(count, 10)
  58.         print("The ten most common words: {0}".format(', '.join(most_common)))
  59.         self.status[3]["text"] = "The ten most common words: {0}".format(', '.join(most_common))
  60.  
  61.     def synchronous_count(self):
  62.         for label in self.status:
  63.             label["text"] = ""
  64.  
  65.         words = load_words("Holmes.txt")
  66.         print("{0} words loaded".format(len(words)))
  67.         self.status[0]["text"] = "{0} words loaded".format(len(words))
  68.  
  69.         self.status[1]["text"] = "Cleaning..."
  70.         words = clean_words(words)
  71.         print("{0} remain after cleaning".format(len(words)))
  72.         self.status[1]["text"] = "{0} remain after cleaning".format(len(words))
  73.  
  74.         self.status[2]["text"] = "Counting..."
  75.         count = count_words(words)
  76.         print("{0} distinct words after counting".format(len(count)))
  77.         self.status[2]["text"] = "{0} distinct words after counting".format(len(count))
  78.  
  79.         self.status[3]["text"] = "Sorting..."
  80.         most_common = get_most_common(count, 10)
  81.         print("The ten most common words: {0}".format(', '.join(most_common)))
  82.         self.status[3]["text"] = "The ten most common words: {0}".format(', '.join(most_common))
  83.  
  84.  
  85. root = tk.Tk()
  86. app = Application(master=root)
  87. with TkContext(app):
  88.     app.mainloop()
RAW Paste Data
Top