# a look at the Tkinter Text widget
# use ctrl+c to copy, ctrl+x to cut selected text,
# ctrl+v to paste, and ctrl+/ to select all
import Tkinter as tk
def most_frequant_word():
# count words in a text and show the first ten items
# by decreasing frequency
# sample text for testing
import sys
import string
import re
v1.set(text1.get(1.0, tk.END))
text1.delete(1.0, tk.END)
file = open ("arb.txt", "r")
text = file.read ( )
file.close ( )
word_freq = {}
word_list = text.split()
for word in word_list:
# word all lower case
word = word.lower()
# strip any trailing period or comma
word = word.rstrip('.,/"-_;\[]()')
# build the dictionary
count = word_freq.get(word, 0)
word_freq[word] = count + 1
# create a list of (freq, word) tuples
freq_list = [(freq, word) for word, freq in word_freq.items()]
# sort the list by the first element in each tuple (default)
freq_list.sort(reverse=True)
for n, tup in enumerate(freq_list):
# print the first ten items
if n < 10:
text1.insert(tk.INSERT, freq)
text1.insert(tk.INSERT, word)
text1.insert(tk.INSERT, "\n")
freq, word = tup
print freq, word
root = tk.Tk(className = " most_frequant_word")
# text entry field, width=width chars, height=lines text
text1 = tk.Text(root, width=50, height=20, bg='green')
text1.pack()
# function listed in command will be executed on button click
button1 = tk.Button(root, text='result', command=most_frequant_word)
button1.pack(pady=5)
# define a variable to hold the label text
v1 = tk.StringVar()
# label text will always be the textvariable's value
# width/height in char size
label1 = tk.Label(root, textvariable=v1, width=50, height=20)
label1.pack(pady=5)
# start cursor in text1.
text1.focus()
root.mainloop()