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 307: 9.19

Answer

code

Work Step by Step

# 9.19 (Move a circle using keys) Write a program that moves a circle up, down, left, or # right using the arrow keys, as shown in Figure 9.30cā€“d. from tkinter import * class MainGUI: def __init__(self): window = Tk() self.cavas = Canvas(window, width=500, height=500, bg="white") self.cavas.pack() self.cavas.create_oval(250, 250, 300, 300, tags="circle") self.cavas.focus_set() self.cavas.bind("", self.move) window.mainloop() def move(self,event): x0, y0, x1, y1 = self.cavas.coords("circle") if event.keysym == "Up": y0 -= 5 y1 -= 5 elif event.keysym == "Down": y0 += 5 y1 += 5 elif event.keysym == "Right": x0 += 5 x1 += 5 elif event.keysym == "Left": x0 -= 5 x1 -= 5 self.cavas.delete("circle") self.cavas.create_oval(x0, y0, x1, y1, tags="circle") 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.