Tkinter Canvas Text adding Border (outline)

时间:2015-10-06 08:17:06

标签: python python-3.x tkinter

I'm having trouble to add borders (outline) in Tkinter Canvas Text, like this:

https://i-msdn.sec.s-msft.com/dynimg/IC147277.jpg

but:

self.text1 = self.canvasFrame.create_text(100, 100, text = "Hello",
    font = NORMAL_FONT, fill = BLUE_COLOR, borderwidth = 2)


File "/usr/lib/python3.4/tkinter/__init__.py", line 2321, in _create
*(args + self._options(cnf, kw))))
_tkinter.TclError: unknown option "-borderwidth"

and I tried "bd = 2" too...

File "/usr/lib/python3.4/tkinter/__init__.py", line 2321, in _create
*(args + self._options(cnf, kw))))
_tkinter.TclError: unknown option "-bd"

Is there any way to do this?

2 个答案:

答案 0 :(得分:3)

不,没有办法做你想做的事。画布的文本项不支持borderwidth选项,或任何其他可以提供所需效果的选项。

答案 1 :(得分:0)

这是一个老问题,但您可以创建如下内容:

from tkinter import Canvas


class NewCanvas(Canvas):
    def create_border_text(self, text="Under developing", borderwidth=None, bordercolor=None, **kw):
            textid = self.create_text(self.winfo_reqwidth() // 2, self.winfo_reqheight() // 2, text=text)
    horizontal_offset, vertical_offset = 40, 40
    bbox = self.bbox(textid)
    new_bbox = (bbox[0] - horizontal_offset // 2, bbox[1] - vertical_offset // 2,
                bbox[2] + horizontal_offset // 2, bbox[3] + vertical_offset // 2)

    self.create_rectangle(*new_bbox, width=borderwidth, outline=bordercolor)
    self.tag_raise(textid)