I integrat program to be GUI using Tkinter I try browser direction
as you can see

# 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
  # count words in a text and show the first ten items
 # by decreasing frequency

import Tkinter as tk
import os, glob
import sys
import string
import re
import tkFileDialog      
def most_frequant_word():    
 a= tkFileDialog.askdirectory()
 browser= os.listdir(a)


 for root, dirs, files in os.walk(browser):
    print 'Looking into %s' % root.split('\\')[-1]
    print 'Found %d dirs and %d files' % (len(dirs), len(files))
 
    for idx, file in enumerate(files):
     ff = open (os.path.join(root, file), "r")
     text = ff.read ( )
     ff.close ( )
     
     word_list = text.strip().split()
     
     for word in word_list:
      word = word.lower().rstrip('.,/"-_;\\[]()')

      if word.isalpha():
                # 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:
        print "%s times: %s" % tup
        text1.insert(tk.INSERT, freq)
        text1.insert(tk.INSERT, word)
        text1.insert(tk.INSERT, "\n")
        
 raw_input('\nHit enter to exit')
 
root = tk.Tk(className = " most_frequant_word")
# text entry field, width=width chars, height=lines text
v1 = tk.StringVar()
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)
text1.focus()
root.mainloop()

which find most 10 words frequancy by search all files in specfic directory

but give me this error

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python25\lib\lib-tk\Tkinter.py", line 1403, in __call__
return self.func(*args)
File "C:\Documents and Settings\Administrator\Desktop\ICS482\hw3\programAli.py", line 21, in most_frequant_word
for root, dirs, files in os.walk(browser):
File "C:\Python25\lib\os.py", line 285, in walk
names = listdir(top)
TypeError: coercing to Unicode: need string or buffer, list found

could you please help me to solve this problem?

I fix the error now
but
it will not insert to the textbox
it just print then hanging

# 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
  # count words in a text and show the first ten items
 # by decreasing frequency

import Tkinter as tk
import os, glob
import sys
import string
import re
import tkFileDialog      
def most_frequant_word():    
 browser= tkFileDialog.askdirectory()
 #browser= os.listdir(a)


 for root, dirs, files in os.walk(browser):
    print 'Looking into %s' % root.split('\\')[-1]
    print 'Found %d dirs and %d files' % (len(dirs), len(files))
    #text1.insert(tk.INSERT,'Looking into %s' % root.split('\\')[-1])
    #text1.insert(tk.INSERT, 'Found %d dirs and %d files' % (len(dirs), len(files)))
    for idx, file in enumerate(files):
     print 'File #%d: %s' % (idx + 1, file)
      #text1.insert(tk.INSERT, 'File #%d: %s' % (idx + 1, file))
     ff = open (os.path.join(root, file), "r")
     text = ff.read ( )
     ff.close ( )
     word_freq = {}
     
     word_list = text.strip().split()
     
     for word in word_list:
      word = word.lower().rstrip('.,/"-_;\\[]()')

      if word.isalpha():
                # 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 < 50:
        print "%s times: %s" % tup
        text1.insert(tk.INSERT, freq)
        text1.insert(tk.INSERT, word)
        text1.insert(tk.INSERT, "\n")
        
 raw_input('\nHit enter to exit')
 
root = tk.Tk(className = " most_frequant_word")
# text entry field, width=width chars, height=lines text
v1 = tk.StringVar()
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='Brows', command=most_frequant_word)
button1.pack(pady=5)
text1.focus()
root.mainloop()

code try to insert to the textbox

print "%s times: %s" % tup
        text1.insert(tk.INSERT, freq)
        text1.insert(tk.INSERT, word)
        text1.insert(tk.INSERT, "\n")

when I wont to insert fil name and directory to the textbox it will hang also
code is comment

print 'Looking into %s' % root.split('\\')[-1]
    print 'Found %d dirs and %d files' % (len(dirs), len(files))
    #text1.insert(tk.INSERT,'Looking into %s' % root.split('\\')[-1])
    #text1.insert(tk.INSERT, 'Found %d dirs and %d files' % (len(dirs), len(files)))
    for idx, file in enumerate(files):
     print 'File #%d: %s' % (idx + 1, file)
      #text1.insert(tk.INSERT, 'File #%d: %s' % (idx + 1, file))
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.