Introduction to Programming using Python 1st Edition

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

Chapter 10 - Lists - Programming Exercises - Page 356: 10.33

Answer

code

Work Step by Step

# 10.33 (Tkinter: draw histograms) Write a program that generates 1,000 lowercase letters # randomly, counts the occurrence of each letter, and displays a histogram for # the occurrences, as shown in Figure 10.16a. import random from tkinter import * class MainGUI: def __init__(self): window = Tk() self.cnvs = Canvas(window, width=550) self.cnvs.pack() self.btn = Button(window, text="Display histogram",command=self.dispHist) self.btn.pack() window.mainloop() def dispHist(self): self.cnvs.delete('all') counts = [0] * 26 for i in range(1000): c = random.randint(0, 25) counts[c] += 1 self.cnvs.create_line(0, 300, 550, 300) x = 0 for i in range(26): c = chr(i + ord('a')) self.cnvs.create_text(x + 20, 305, text=c) x += 20 height = counts[i]*200/max(counts) self.cnvs.create_rectangle(x - 10, 300, x + 10, height) MainGUI()
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.