如何让文本出现在框的前面?

How can I make text appear in front of a box?

我 运行 在 Python 中使用 TkInter 的一些基本 GUI 代码:

# Making a blank canvas to "draw" on, it can easily be seen as it will be white
canvas = Canvas(root, width=500, height=500, background="white")
canvas.grid(row=6, column=5)

# Creating text to go within the boxes
officeText = canvas.create_text(125, 110, text="Office")

# Creating boxes within the canvas
officeGraphic = canvas.create_rectangle(100, 100, 150, 150, fill="orange")

但是我遇到的问题是文本 "office" 出现在橙色框后面。我怎样才能把这段文字放在前面?

TK 将按照小部件的创建顺序绘制小部件,最后创建的小部件位于之前创建的小部件之上。使用该逻辑,您可以简单地将 officeText 向下移动:

# Creating boxes within the canvas
officeGraphic = canvas.create_rectangle(100, 100, 150, 150, fill="orange")

# Creating text to go within the boxes
officeText = canvas.create_text(125, 110, text="Office")

如果无法更改创建顺序,您可以使用 cavas 的 tag_raise 方法。

canvas.tag_raise(officeText)