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.13

Answer

code

Work Step by Step

# 9.13 (Display the mouse position) Write two programs: one that displays the mouse # position when the mouse is clicked (see Figure 9.28a–b), and the other displays # the mouse position when the mouse button is pressed and ceases to display it when # the mouse button is released. from tkinter import * class MainGUI1: def __init__(self): window = Tk() self.canvas = Canvas(window, bg="white", width=300, height=300) self.canvas.pack() self.canvas.bind("", self.dispPos) window.mainloop() def dispPos(self, event): self.canvas.delete("txt") x = event.x y = event.y txt = "(" + str(x) + " , " + str(y) + ")" self.canvas.create_text(x, y, text=txt, tags="txt") class MainGUI2: def __init__(self): window = Tk() self.canvas = Canvas(window, bg="white", width=300, height=300) self.canvas.pack() self.canvas.bind("", self.dispPos) self.canvas.bind("", self.deletPos) window.mainloop() def dispPos(self, event): x = event.x y = event.y txt = "(" + str(x) + " , " + str(y) + ")" self.canvas.create_text(x, y, text=txt, tags="txt") def deletPos(self, event): self.canvas.delete("txt") MainGUI2()
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.