Tkinter,Label/Text 在 canvas.rectangle [python]
Tkinter, Label/Text in canvas.rectangle [python]
我需要在 tkinter 中以 canvas 矩形为中心放置一个 text/label。
首先我有一个 canvas 覆盖整个屏幕 (800, 600)。
然后我有几个矩形,它们是使用以下方法制作的:
create_rectangle(...)
第一个矩形的第一个X是275,第二个X是525。
第一个矩形的第一个Y为265,第二个Y为315。
menuBtn1 = canvas.create_rectangle(275, 165, 525, 215, fill="#C2B6BF")
现在如何在这个矩形的中心放置一个 text/label?
你应该使用 create_text。正如position参数描述中的link中所说:
By default, the text
is centered on this position. You can override this with the anchor
option. For example, if the coordinate is the upper left corner, set
the anchor to NW.
所以这应该是你想要的:
mylabel = canvas.create_text((400, 190), text="Label text")
我需要在 tkinter 中以 canvas 矩形为中心放置一个 text/label。
首先我有一个 canvas 覆盖整个屏幕 (800, 600)。 然后我有几个矩形,它们是使用以下方法制作的:
create_rectangle(...)
第一个矩形的第一个X是275,第二个X是525。
第一个矩形的第一个Y为265,第二个Y为315。
menuBtn1 = canvas.create_rectangle(275, 165, 525, 215, fill="#C2B6BF")
现在如何在这个矩形的中心放置一个 text/label?
你应该使用 create_text。正如position参数描述中的link中所说:
By default, the text is centered on this position. You can override this with the anchor option. For example, if the coordinate is the upper left corner, set the anchor to NW.
所以这应该是你想要的:
mylabel = canvas.create_text((400, 190), text="Label text")