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 309: 9.26

Answer

code

Work Step by Step

# 9.26 (Display balls with random colors) Write a program that displays ten balls with # random colors and placed at random locations, at shown in Figure 9.36c. from tkinter import * # Import tkinter from random import randint width = 300 height = 150 radius = 4 # Convert an integer to a single hex digit in a character def toHexChar(hexValue): if hexValue <= 9 and hexValue >= 0: return chr(hexValue + ord('0')) else: # hexValue <= 15 && hexValue >= 10 return chr(hexValue - 10 + ord('A')) class MainGUI: def __init__(self): window = Tk() # Create a window window.title("Random Balls") # Set title x = 100 y = 100 self.canvas = Canvas(window, width=width, height=height) self.canvas.pack() Button(window, text="Display", command=self.display).pack() window.mainloop() # Create an event loop def display(self): self.canvas.delete("ball") for i in range(10): x = randint(0, width - 1) y = randint(0, height - 1) color = "#" for j in range(6): color += toHexChar(randint(0, 15)) self.canvas.create_oval(x - radius, y - radius, x + radius, y + radius, fill=color, tags="ball") 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.