Introduction to Programming using Python 1st Edition

Published by Pearson
ISBN 10: 0132747189
ISBN 13: 978-0-13274-718-9

Chapter 14 - Tuples, Sets, and Dictionaries - Programming Exercises - Page 497: 14.7

Answer

code

Work Step by Step

# 14.7 (Tkinter: Count the occurrences of each letter) Revise the preceding exercise to # display a histogram for the result, as shown in Figure 14.6. You need to display a # message in a message box if the URL does not exist. import urllib.request from tkinter import * from tkinter import messagebox LINE_WIDTH = 23 BASE = 550 def showResult(): try: file = urllib.request.urlopen(url.get()) lines = file.read().decode() counts = [0] * 26 countLetters(lines.lower(), counts) cnvs.delete("data") cnvs.create_line(10, BASE, 595, BASE, tag="data") x = 12 for i in range(26): cnvs.create_text(x + 2, BASE + 8, text=chr(i + ord('a')), tag="data") cnvs.create_rectangle(x - 9, BASE - counts[i], x + LINE_WIDTH - 9, BASE, tag="data") x += LINE_WIDTH except IOError: messagebox.showwarning("Analyze URL", "URL " + url.get() + " does not exist") def countLetters(lines, counts): for line in lines: for ch in line: if ch.isalpha(): counts[ord(ch) - ord('a')] += 1 window = Tk() # Create a window window.title("Occurrence of Letters") # Set title frame1 = Frame(window) # Hold four labels for displaying cards frame1.pack() cnvs = Canvas(frame1, width=600, height=600) cnvs.pack() frame2 = Frame(window) # Hold four labels for displaying cards frame2.pack() Label(frame2, text="Enter a URL: ").pack(side=LEFT) url = StringVar() Entry(frame2, width=20, textvariable=url).pack(side=LEFT) Button(frame2, text="Show Result", command=showResult).pack(side=LEFT) window.mainloop() # Create an event loop
Update this answer!

You can help us out by revising, improving and updating this answer.

Update this answer

After you claim an answer you’ll have 24 hours to send in a draft. An editor will review the submission and either publish your submission or provide feedback.