简体   繁体   English

如何知道python中海龟图形上特定文本的像素大小?

[英]How to know the pixel size of a specific text on turtle graphics in python?

As title, when I execute the following code作为标题,当我执行以下代码时


import turtle
turtle.write("some text")

I want to know the whole size (including height and width) of the string some text on the canvas of the turtle graphics.我想知道乌龟图形画布上some text的字符串的整体大小(包括高度和宽度)。
How can I do that?我该怎么做?

The font size only tells you half of what you need to know, ie the height:字体大小只告诉你一半你需要知道的,即高度:

The size of a font is typically taken to be the distance from the top of the highest character to the bottom of the lowest character.字体的大小通常被认为是从最高字符的顶部到最低字符的底部的距离。

From FontSize.html来自FontSize.html

But we can get the width via setting the move= option of turtle.write() to True .但是我们可以通过将turtle.write()move=选项设置为True来获得宽度。 Here's a example where I want to draw a box around the text I've just drawn:这是一个示例,我想在我刚刚绘制的文本周围绘制一个框:

from turtle import Turtle, Screen
from tkinter.font import Font

TEXT = "Penny for your thoughts"  # arbitrary text
POSITION = (150, 150)  # arbitrary position

FONT_SIZE = 36  # arbitrary font size
FONT = ('Arial', FONT_SIZE, 'normal')  # arbitrary font

X, Y = 0, 1

def box(turtle, lower_left, upper_right):
    """ Draw a box but clean up after ourselves """

    position = turtle.position()
    isdown = turtle.isdown()

    if isdown:
        turtle.penup()

    turtle.goto(lower_left)
    turtle.pendown()
    turtle.goto(upper_right[X], lower_left[Y])
    turtle.goto(upper_right)
    turtle.goto(lower_left[X], upper_right[Y])
    turtle.goto(lower_left)

    turtle.penup()
    turtle.setposition(position)

    if isdown:
        turtle.pendown()

screen = Screen()

marker = Turtle(visible=False)
marker.penup()
marker.goto(POSITION)

start = marker.position()
marker.write(TEXT, align='center', move=True, font=FONT)
end = marker.position()

font_config = Font(font=FONT)
font_ascent = font_config.metrics('ascent')
buffer = (font_config.metrics('linespace') - font_ascent) / 2

# Since it's centered, the end[X] - start[X] represents 1/2 the width
box(marker, (2 * start[X] - end[X], start[Y] - buffer), (end[X], start[Y] + font_ascent + buffer))

screen.exitonclick()

在此处输入图片说明

Now, here's an example that draws the box first, fills it, and then draws the text into it:现在,这是一个先绘制框,填充它,然后将文本绘制到其中的示例:

from turtle import Turtle, Screen
from tkinter.font import Font

TEXT = "Penny for your thoughts"  # arbitrary text
POSITION = (150, 150)  # arbitrary position

FONT_SIZE = 36  # arbitrary font size
FONT = ('Arial', FONT_SIZE, 'normal')  # arbitrary font

X, Y = 0, 1

# def box(turtle, lower_left, upper_right):
#     """ same as above example """
def box(turtle, lower_left, upper_right):
    """ Draw a box but clean up after ourselves """

    position = turtle.position()
    isdown = turtle.isdown()

    if isdown:
        turtle.penup()

    turtle.goto(lower_left)
    turtle.pendown()
    turtle.goto(upper_right[X], lower_left[Y])
    turtle.goto(upper_right)
    turtle.goto(lower_left[X], upper_right[Y])
    turtle.goto(lower_left)

    turtle.penup()
    turtle.setposition(position)

    if isdown:
        turtle.pendown()

screen = Screen()

font_config = Font(font=FONT)
font_ascent = font_config.metrics('ascent')
buffer = (font_config.metrics('linespace') - font_ascent) / 2
text_width = font_config.measure(TEXT)

marker = Turtle(visible=False)
marker.penup()
marker.fillcolor('pink')
marker.goto(POSITION)

# Since it's centered, we need to work with half widths
half_width = text_width / 2
marker.begin_fill()
box(marker, (POSITION[X] - half_width, POSITION[Y] - buffer), (POSITION[X] + half_width, POSITION[Y] + font_ascent + buffer))
marker.end_fill()

marker.write(TEXT, align='center', font=FONT)

screen.exitonclick()

在此处输入图片说明

The default font for turtle.write is 'Arial' with a font-size of 8px as described in the documentation https://docs.python.org/3/library/turtle.html#turtle.write . turtle.write的默认字体是“Arial”,字体大小为 8px,如文档https://docs.python.org/3/library/turtle.html#turtle.write 中所述

The height and width of the text is dependent on the font parameter.文本的高度和宽度取决于font参数。

Though Brandon pretty much gave the answer, let me give you an example:尽管布兰登几乎给出了答案,但让我举个例子:

>>>>import turtle
>>>>turtle.write('hey hello this is DSP',font=('consolas',8,'bold'))

This would do your job, Consolas is the font type, 8 is the font's size, and bold is the font type.这可以完成您的工作,Consolas 是字体类型,8 是字体大小,粗体是字体类型。 Other than bold you can go for italic or normal as well.除了粗体,您还可以选择斜体或正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM