如何让文字出现在盒子前面?

时间:2015-02-12 12:42:19

标签: python tkinter

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

# 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"出现在橙色框后面。如何将此文本带到前面?

2 个答案:

答案 0 :(得分:2)

如果不能选择更改创作顺序,可以使用cavas' tag_raise方法。

canvas.tag_raise(officeText)

答案 1 :(得分:1)

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