B.Johnson
B.Johnson

Reputation: 23

Writing a code for a Canvas window in Python

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.

Upvotes: 1

Views: 896

Answers (2)

Helder Sepulveda
Helder Sepulveda

Reputation: 17594

Here is a working version of your code:

from tkinter import *

class MyCanvas:
    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")

MyCanvas()
  • changed your class name to avoid conflicts
  • commented btClear since that is not defined

And here is how it looks:


If you use Visual Studio code all these syntax errors are really easy to spot and fix:

Upvotes: 1

user12066865
user12066865

Reputation:

I know the error,

self.canvas = Canvas(window, width = 200, height = 100, bg = "white")

Your class name is Canvas and you are using tkinter.Canvas which makes tkinter.Canvas become Canvas class

So, the right code is to change Canvas to tkinter.Canvas in the code and DO NOT TYPE TKINTER.CANVAS AS CANVAS BECAUSE IT WAS DEFINED AS CLASS

Or you can change your classname to CreateCanvas, not Canvas

class CreateCanvas():

Upvotes: 0

Related Questions