Introduction to Programming using Python 1st Edition

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

Chapter 9 - GUI Programming using Tkinter - Programming Exercises - Page 306: 9.12

Answer

code

Work Step by Step

# 9.12 (Alternate two messages) Write a program to change, with a left mouse click, # between two messages displayed on a canvas, “Programming is fun” and “It is fun # to program,” as shown in Figure 9.27b–c. from tkinter import * # Import tkinter width = 220 height = 100 class MainGUI: def __init__(self): window = Tk() # Create a window window.title("Rotating Message") # Set a title self.on = False self.canvas = Canvas(window, bg="white", width=width, height=height) self.canvas.pack() self.canvas.create_text(width / 2, height / 2, text="Programming is fun", tags="text") # Bind canvas with mouse events self.canvas.bind("", self.rotate) window.mainloop() # Create an event loop def rotate(self, event): self.canvas.delete("text") if self.on: self.canvas.create_text(width / 2, height / 2, text="Programming is fun", tags="text") else: self.canvas.create_text(width / 2, height / 2, text="It is fun to program", tags="text") self.on = not self.on 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.