Question
This question is asked multiple times on this site, and all the answers have multiple thumbs down because none of the answers work. The ones
This question is asked multiple times on this site, and all the answers have multiple thumbs down because none of the answers work. The ones that do work do other things like count the number of words or list the frequency of the words or other stuff. The one that doesn't work is pasted below. Using Tkinter: Count the occurrences of each letter using a GUI program, let the user enter a file from the entry field or by selecting a file using the browse button to display an open file dialog box. The file selected is then displayed in the entry field. By clicking the show result button displays the result in a text widget. You need to display a message in a message box if the file does not exist. The show result button should show "a appears 102 times" kind of result for each letter. Here is the code that keeps showing up on the site that does not work:
from tkinter import * # Import tkinter import tkinter.messagebox # Import tkinter.messagebox from tkinter.filedialog import askopenfilename
def showResult(): analyzeFile(filename.get())
def analyzeFile(filename): try: infile = open(filename, "r") # Open the file counts = 26 * [0] # Create and initialize counts for line in infile: # Invoke the countLetters function to count each letter countLetters(line.lower(), counts) # Display results for i in range(len(counts)): if counts[i] != 0: text.insert(END, chr(ord('a') + i) + " appears " + str(counts[i]) + (" time" if counts[i] == 1 else " times") + " ") infile.close() # Close file except IOError: tkinter.messagebox.showwarning("Analyze File", "File " + filename + " does not exist") # Count each letter in the string def countLetters(line, counts): for ch in line: if ch.isalpha(): counts[ord(ch) - ord('a')] += 1 def openFile(): filenameforReading = askopenfilename() filename.set(filenameforReading) window = Tk() # Create a window window.title("Occurrence of Letters") # Set title
frame1 = Frame(window) # Hold four labels for displaying cards frame1.pack()
scrollbar = Scrollbar(frame1) scrollbar.pack(side = RIGHT, fill = Y) text = Text(frame1, width = 40, height = 10, wrap = WORD, yscrollcommand = scrollbar.set) text.pack() scrollbar.config(command = text.yview)
frame2 = Frame(window) # Hold four labels for displaying cards frame2.pack()
Label(frame2, text = "Enter a filename: ").pack(side = LEFT) filename = StringVar() Entry(frame2, width = 20, textvariable = filename).pack(side = LEFT) Button(frame2, text = "Browse", command = openFile).pack(side = LEFT) Button(frame2, text = "Show Result", command = showResult).pack(side = LEFT)
window.mainloop() # Create an event loop
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started