Reputation: 4130
I'm trying to set a background colour for a Tkinter text widget - I'm trying to copy the example here and here, both of which seem in indicate that I can do this:
w.create_text(*textSet, text=i[3], font=("Helvetica", 16), bg="white"))
But when I try this, I get an error from Tkinker:
w.create_text(*textSet, text=i[3], font=("Helvetica", 16), bg="white")
File "C:\python27\lib\lib-tk\Tkinter.py", line 2213, in create_text
return self._create('text', args, kw)
File "C:\python27\lib\lib-tk\Tkinter.py", line 2189, in _create
*(args + self._options(cnf, kw))))
_tkinter.TclError: unknown option "-bg"
I tried with the key 'background' with the same result.
The text needs to be overlaid on a circle, the circle size is dynamically generated so when the circle is smaller than the text, I want a solid background so the line for the circle doesn't disrupt the text.
Any pointer to what I'm doing wrong? This is the whole section:
master = Tk()
w = Canvas(master, width=1000, height=1000)
w.config(bg='white')
w.pack()
w.create_oval(*coordsSet, width=3)
w.create_text(*textSet, text=i[3], font=("Helvetica", 16), bg="white")
mainloop()
Upvotes: 4
Views: 15800
Reputation: 2162
Unfortunately create_text doesn't support that option. You can either overlay a tkInter text widget which does support bg and is described in your second link. Alternatively, you can use the bbox function to get the bounding box of the text and then overlay a white rectangle under the text which would have a similar effect.
Example of the second approach:
i=w.create_text(*textSet, text=i[3], font=("Helvetica", 16))
r=w.create_rectangle(w.bbox(i),fill="white")
w.tag_lower(r,i)
Upvotes: 9