Introduction to Programming using Python 1st Edition

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

Chapter 12 - Inheritance and Polymorphism - Programming Exercises - Page 438: 12.22

Answer

code

Work Step by Step

# 12.22 (Flip coins) Write a program that displays heads (H) or tails (T) for each of nine # coins, as shown in Figure 12.31c–d. When a cell is clicked, the coin is flipped. # Write a custom cell class that extends Label. In the initializer of the class, bind # the event with the method for flipping the coin. When the program # starts, all cells initially display H. from tkinter import * # Import tkinter class CoinLabel(Label): def __init__(self, container, text): Label.__init__(self, container, text=text, font="Helvetica 30 bold") self.bind("", self.flip) def flip(self, event): if self["text"] == "H": self["text"] = "T" else: self["text"] = "H" class MainClass: def __init__(self): window = Tk() # Create a window window.title("Flip Coin") # Set title frame = Frame(window) frame.pack() for i in range(3): for j in range(3): CoinLabel(frame, text="H").grid(row=i, column=j) window.mainloop() # Create an event loop MainClass()
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.