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 305: 9.6

Answer

code

Work Step by Step

# 9.6 (Game: display a tic-tac-toe board ) Write a program that displays nine labels. # Each label may display an image icon for an X or an image icon for an O, as # shown in Figure 9.25b. What to display is randomly decided. Use the # random.randint(0, 1) function to generate an integer 0 or 1, which corresponds # to displaying a cross image (X) icon or a not image (O) icon. The cross and # not images are in the files x.gif and o.gif. import random from tkinter import * window = Tk() x = PhotoImage(file="x.gif") o = PhotoImage(file="o.gif") for i in range(3): for j in range(3): r = random.randint(0, 1) if r == 0: Label(window, image=x).grid(row=i + 1, column=j + 1) elif r == 1: Label(window, image=o).grid(row=i + 1, column=j + 1) window.mainloop()
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.