如何使用tkinter在可见背景上插入文本?

时间:2014-11-16 18:26:48

标签: python

如何在不阻挡图像背景的情况下在画布上插入文字?我知道这是一个新手问题,但我无法找到答案。

from tkinter import *
from PIL import ImageTk

master = Tk()

canvas = Canvas(master, width=1000, height=1000)
canvas.pack()

image = ImageTk.PhotoImage(file="image.png")
canvas.create_image(0, 0, image=image, anchor=NW)

canvas.create_text(2, 2, text="Python")

mainloop()

编辑:看起来我终于明白了(感谢@Jasper)。

from tkinter import *
from PIL import ImageTk

master = Tk()

#width, height = Image.open(image.png).size

canvas = Canvas(master, width=900, height=563)
canvas.pack()

image = ImageTk.PhotoImage(file="image.png")
canvas.create_image(0, 0, image=image, anchor=NW)

canvas_id = canvas.create_text(10, 10, anchor="nw")
canvas.itemconfig(canvas_id, text="this is the text "*300, width=780)
canvas.itemconfig(canvas_id, font=("courier", 12))
canvas.insert(canvas_id, 12, "new ")

#canvas.create_text(2, 2, text="Python")

mainloop()

0 个答案:

没有答案