0

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?

1 Answer 1

1

Try explicitly setting values for your StringVars before you enter the mainloop.

self.v1.set("1")
self.v2.set("0")

Unchecking the "fill" button doesn't cause a hollow shape to be drawn, because hollow shapes are only drawn during displayRect and displayOval, but you aren't calling either one of those from within processFill. I suggest just having a single function that serves as the callback for all three of your widgets.

    self.v1 = StringVar()
    rbRect = Radiobutton(frame, text = "Rectangle", command = self.optionChanged, variable = self.v1, value = '1')
    rbOval = Radiobutton(frame, text = "Oval", command = self.optionChanged, variable = self.v1, value = '2')
    self.v2 = StringVar()
    cbtFill = Checkbutton(frame, text = "Fill", command = self.optionChanged, variable = self.v2)

#later on in the class definition...
def optionChanged(self):
    self.canvas.delete("rect", "oval")
    color = "white" if self.v2.get() == "0" else "red"
    if self.v1.get() == '1':
        self.canvas.create_rectangle(10, 10, 190, 90, tags = "rect", fill = color)
    else:
       self.canvas.create_oval(10, 10, 190, 90, tags = "oval", fill = color)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.