0

I'm trying to write a code that prints a Frame to the screen with a Button and Canvas in it

import tkinter as tk
class SampleApp(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.entry = tk.Entry(self)
        self.button = tk.Button(self, text="Get", command=self.on_button)
        self.button.pack()
        self.entry.pack()
        self.text =tk.Text(height=20,width=10)
        self.text.pack()
        self.canvas=tk.Canvas(fill='Black')
        self.canvas.pack()
    def on_button(self):
        print(self.entry.get())
app = SampleApp()
app.mainloop()

As soon as I run it, I get an error:

_tkinter.TclError: unknown option "-fill"

I have no idea why.

1
  • "I have no idea why." < really?! The error message seems pretty unambiguous (and, as you see the full traceback, tells you exactly which line is the problem): tk.Canvas(fill='Black') has an option, fill, that isn't known (see e.g. effbot.org/tkinterbook/canvas.htm#Tkinter.Canvas.config-method). Commented Apr 7, 2015 at 13:11

2 Answers 2

1

Fill is a create_rectangle argument, not a constructor argument:

self.canvas.create_rectangle(0, 0, width, height, fill = "black") 
Sign up to request clarification or add additional context in comments.

Comments

1

The canvas doesn't use a fill option to define what the background is; the error is coming out of the lower levels of the Tkinter code where it flips to the underlying Tcl/Tk runtime; option names get a hyphen put in front of them, and the error otherwise means what it says, “don't know what fill is in this context” (paraphrased).

However, the canvas does use a background option that takes a colour. Try:

    self.canvas=tk.Canvas(background='Black')

You can also create rectangles on the canvas; those are fillable. The overall canvas isn't a rectangle, it's a widget.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.