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

Answer

code

Work Step by Step

# 9.17 (Racing car) Write a program that simulates car racing, as shown in Figure # 9.29b–d. The car moves from left to right. When it reaches the right end, it restarts # from the left and continues the same process. Let the user increase and decrease # the car’s speed by pressing the Up and Down arrow keys. from tkinter import * class MainGUI: def __init__(self): window = Tk() self.canvas = Canvas(window, width=250, height=90, bg="white") self.canvas.bind("", self.incSpeed) self.canvas.bind("", self.decSpeed) self.canvas.pack() self.car = PhotoImage(file="car.gif") x = 0 self.canvas.create_image(x, 50, image=self.car, tags="car") self.canvas.focus_set() self.dx = 10 while True: self.canvas.move("car", self.dx, 0) self.canvas.after(100) self.canvas.update() if x < 250: x += self.dx else: x = 0 self.canvas.delete("car") self.canvas.create_image(x, 50, image=self.car, tags="car") window.mainloop() def incSpeed(self, event): if self.dx < 40: self.dx += 5 def decSpeed(self, event): if self.dx > 5: self.dx -= 5 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.