So I'm trying to get Canvas to display a window with buttons that dislpay the shapes when pressed (I'm also trying to get it to gave a checkbox that fills in the shapes but that's not important at the moment) but every time I run the program it displays stuff like:
Traceback (most recent call last): File "C:\Users\Brandons\Desktop\school files\Sdev 220 Python\9.3 geometricfigures.py", line 35, in Canvas() File "C:\Users\Brandons\Desktop\school files\Sdev 220 Python\9.3 geometricfigures.py", line 13, in init self.canvas = Canvas(window, width = 200, height = 100, bg = "white") TypeError: init() got an unexpected keyword argument 'width'
So when that happened I tried to delete that info because I thought that it was possibly interfering with it but then it continued to say that the arguments height and bg were unexpected keyword arguements so I deleted them too. After deleting them I ran the program with only the window keyword left but this time it showed:
Traceback (most recent call last): File "C:\Users\Brandons\Desktop\school files\Sdev 220 Python\9.3 geometricfigures.py", line 35, in Canvas() File "C:\Users\Brandons\Desktop\school files\Sdev 220 Python\9.3 geometricfigures.py", line 13, in init self.canvas = Canvas(window) TypeError: init() takes 1 positional argument but 2 were given
In the end I also deleted the window keyword as well. But when I did that it ran the program but nothing would show up for some reason, not even an error or anything. So now I'm not sure what to do since I'm certain that stuff needs to be in there but I'm not sure how to implement it without causing an error and have it actually display the window and buttons.
from tkinter import *
class Canvas:
def __init__(self):
window = Tk()
window.title("Canvas")
self.canvas = Canvas(window, width = 200, height = 100, bg = "white")
self.canvas.pack()
frame = Frame(window)
frame.pack()
btRectangle = Button(frame, text = "Rectangle", command = self.displayRect)
btOval = Button(frame, text = "Oval", command = self.displayOval)
btRectangle.grid(row = 1, column = 1)
btOval.grid(row = 1, column = 2)
btClear.grid(row = 1, column = 3)
window.mainloop()
def displayRect(self):
self.canvas.create_rectangle(110, 10, 210, 80, outline = "black", tags = "rect")
def displayOval(self):
self.canvas.create_oval(110, 10, 210, 80, outline = "black", tags = "oval")
def clearCanvas(self):
self.canvas.delete("rect", "oval")
Canvas()
EDIT
Ok so I managed to figure out thanks to the feedback I was given but now I'm having some trouble with filling in the shapes that are created when I selected the check box I created.
from tkinter import *
class GeofigGUI:
def __init__(self):
window = Tk()
window.title("Geometry Figures")
self.canvas = Canvas(window, width = 300, height = 200, bg = "white")
self.canvas.pack()
frame = Frame(window)
frame.pack()
self.v1 = IntVar()
cbtFill = Checkbutton(frame, text = "Fill", variable = self.v1, command = self.fillCheckbutton)
self.v2 = IntVar()
btRectangle = Button(frame, text = "Rectangle", command = self.displayRect)
btOval = Button(frame, text = "Oval", command = self.displayOval)
btClear = Button(frame, text = "Clear", command = self.clearCanvas)
btRectangle.grid(row = 1, column = 1)
btOval.grid(row = 1, column = 2)
btClear.grid(row = 1, column = 3)
cbtFill.grid(row = 1, column = 4)
window.mainloop()
def displayRect(self):
self.canvas.create_rectangle(110, 10, 210, 80, outline = "black", tags = "rect")
def displayOval(self):
self.canvas.create_oval(110, 10, 210, 80, outline = "black", tags = "oval")
def clearCanvas(self):
self.canvas.delete("rect", "oval")
def fillCheckbutton(self):
if self.v2.get() == 1:
self.canvas.itemconfigure(self.displayRect, fill = "black")
self.canvas.itemconfigure(self.displayOval, fill = "black")
else:
self.canvas.itemconfigure(self.displayRect, fill = 'white')
self.canvas.itemconfigure(self.displayOval, fill = "white")
GeofigGUI()
when I run the program it all works except for the checkbox not changing the color of the shapes I make. I can't find anything in the book where something like that occurs involving changing the color of something that is already in the code.


self.v2doesn't point to theCheckbuttonyou created. The variable you used isself.v1. Secondly, you are trying to callitemconfigureon a method, which also doesn't do anything. Since you already created tags for the shapes, you should pass the tag instead, i.e.self.canvas.itemconfigure("rect", fill = "black").