Tkinter, Label/Text in canvas.rectangle [python]

寵の児 提交于 2021-01-28 19:45:16

问题


I need to place a text/label centred in a canvas rectangle in tkinter.

First I have a canvas covering the whole screen(800, 600). And then I have a couple of rectangles which I made using the:

create_rectangle(...)

The first X of the first rectangle is 275 and the second X is 525.

The first Y of the first rectangle is 265 and the second Y is 315.

menuBtn1 = canvas.create_rectangle(275, 165, 525, 215, fill="#C2B6BF")

Now how I can place a text/label in the center of this rectangle?


回答1:


You should use create_text. As it says in the link in the description of the position parameter:

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.

So this should be what you want:

mylabel = canvas.create_text((400, 190), text="Label text")


来源:https://stackoverflow.com/questions/39087139/tkinter-label-text-in-canvas-rectangle-python

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