How to put an outline on a canvas text on python — tkinter?

半城伤御伤魂 提交于 2020-05-29 11:40:42

问题


I've created a white text in the center of my canvas, but my background is very colorful and one part of it is a very light color, so some corners of my sentence doesn't appear. I can't find any options to set borders or an outline. what could I do?


回答1:


Create a text item, get the bounding box of that item, use that data to create a rectangle, and raise the text above the rectangle.

import Tkinter as tk

root = tk.Tk()
canvas = tk.Canvas(root, background="white")
canvas.pack(fill="both", expand=True)

text_item = canvas.create_text(20, 20, anchor="w", text="Hello world!", fill="white")
bbox = canvas.bbox(text_item)
rect_item = canvas.create_rectangle(bbox, outline="red", fill="black")
canvas.tag_raise(text_item,rect_item)

root.mainloop()


来源:https://stackoverflow.com/questions/37139646/how-to-put-an-outline-on-a-canvas-text-on-python-tkinter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!