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 304: 9.3

Answer

code

Work Step by Step

# 9.3 (Select geometric figures) Write a program that draws a rectangle or an oval, as # shown in Figure 9.23. The user selects a figure from a radio button and specifies # whether it is filled by selecting a check button. from tkinter import * class GUI: def __init__(self): window = Tk() self.v1 = IntVar() self.v2 = IntVar() self.c = Canvas(window, width=200, height=200, bg="white") self.c.pack() f = Frame(window) rrb = Radiobutton(f, text="Rectangle", variable=self.v1, value=1, command=self.draw).grid(row=1, column=1) orb = Radiobutton(f, text="Oval", variable=self.v1, value=2, command=self.draw).grid(row=1, column=2) fillcb = Checkbutton(f, text="Filled", variable=self.v2).grid(row=1, column=3) f.pack() window.mainloop() def draw(self): self.c.delete("r","o") if self.v2.get() == 1: if self.v1.get() == 1: self.c.create_rectangle(10, 10, 200, 100, fill="blue", tag="r") else: self.c.create_oval(10, 10, 200, 100, fill="blue", tag="o") else: if self.v1.get() == 1: self.c.create_rectangle(10, 10, 200, 100,tag="r") else: self.c.create_oval(10, 10, 200, 100,tag="o") GUI()
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.