I'm using python 3.5.1 for a Programming with Python class.
The assignment is:
(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 is it filled by selecting a check button.
from tkinter import *
class geometricFig:
def __init__(self):
window = Tk()
window.title("Geometric Figures")
self.canvas = Canvas(window, width = 200, height = 100, bg = "white")
self.canvas.pack()
frame = Frame(window)
frame.pack()
self.v1 = StringVar()
rbRect = Radiobutton(frame, text = "Rectangle", command = self.displayRect, variable = self.v1, value = '1')
rbOval = Radiobutton(frame, text = "Oval", command = self.displayOval, variable = self.v1, value = '2')
self.v2 = StringVar()
cbtFill = Checkbutton(frame, text = "Fill", command = self.processFill, variable = self.v2)
rbRect.grid(row = 1, column = 1)
rbOval.grid(row = 1, column = 2)
cbtFill.grid(row = 1, column = 3)
window.mainloop()
def displayRect(self):
self.canvas.delete("rect", "oval")
self.canvas.create_rectangle(10, 10, 190, 90, tags = "rect")
def displayOval(self):
self.canvas.delete("rect", "oval")
self.canvas.create_oval(10, 10, 190, 90, tags = "oval")
def processFill(self):
if self.v1.get() == '1':
self.canvas.delete("rect", "oval")
self.canvas.create_rectangle(10, 10, 190, 90, tags = "rect", fill = "red")
elif self.v1.get() == '2':
self.canvas.delete("rect", "oval")
self.canvas.create_oval(10, 10, 190, 90, tags = "oval", fill = "red")
else:
self.canvas.delete("rect", "oval")
geometricFig()
What I was able to do was create the window and have the radio buttons draw the rectangle and oval. I was also able to implement the check button to draw a filled rectangle or oval, depending on which radio button was ticked.
However, when I build the program, it starts the program with all radio and check buttons filled in. I have to uncheck everything for the program to start working properly. Also, when I uncheck the fill button, it does not unfill the shapes, but leaves a filled shape on the canvas.
What can I do to fix these problems?