问题
I understand that it is possible to get the size of text on a canvas:
t = canvas.create_text(x, y, text='Hello StackOverflow')
print(canvas.bbox(t))
but I want to decide where to draw the text based on its size, so I need to know the size before the text is drawn. How to do that?
回答1:
A possible solution is
tk.font.Font(size=9, family='Helvetica').measure('HelloStackoverflow')
回答2:
Use that line of code.
canvas.create_text(245,100, fill = 'red', font = "Times 20 italic bold", text = "Hello StackOverflow" )
Here 245,100 is the center of the text box.
fill = 'red' it is text color
font = "Times 20 italic bold" # text size is 20 pixel and it is italic font.
来源:https://stackoverflow.com/questions/53449660/tkinter-canvas-create-text-size-python3