Python Tkinter text background

10,298

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)
Share:
10,298
Jay Gattuso
Author by

Jay Gattuso

I'm not a coder by any stretch of the imagination. I'm trying to learn, solo, and I always really appreciate any help!

Updated on June 04, 2022

Comments

  • Jay Gattuso
    Jay Gattuso almost 2 years

    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()