Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何知道python中海龟图形上特定文本的像素大小?_Python_Python 3.x_Turtle Graphics - Fatal编程技术网

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

如何知道python中海龟图形上特定文本的像素大小?,python,python-3.x,turtle-graphics,Python,Python 3.x,Turtle Graphics,作为标题,当我执行以下代码时 import turtle turtle.write("some text") 我想知道海龟图形画布上的字符串一些文本的整体大小(包括高度和宽度)。 我该怎么做?turtle.write的默认字体是“Arial”,字体大小为8px,如文档中所述 文本的高度和宽度取决于font参数。虽然Brandon给出了答案,但让我举个例子: >>>>import turtle >>>>turtle.write('hey hell

作为标题,当我执行以下代码时


import turtle
turtle.write("some text")
我想知道海龟图形画布上的字符串
一些文本的整体大小(包括高度和宽度)。

我该怎么做?

turtle.write的默认字体是“Arial”,字体大小为8px,如文档中所述


文本的高度和宽度取决于
font
参数。

虽然Brandon给出了答案,但让我举个例子:

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

这将完成您的工作,ConsoleAs是字体类型,8是字体大小,粗体是字体类型。除了粗体,您还可以选择斜体或普通。

字体大小只告诉您需要知道的内容的一半,即高度:

字体的大小通常被视为距顶部的距离 从最高字符到最低字符的底部

但是我们可以通过将
turtle.write()
move=
选项设置为
True
来获得宽度。下面是一个示例,我想在刚才绘制的文本周围绘制一个框:

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

下面是一个示例,它首先绘制框,填充框,然后将文本绘制到框中:

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

我认为这是不可能的
turtle
是一个玩具模块,功能非常有限。因此,我可以通过
len(string)*8
估算宽度?我找到了关于tkinter的类似解决方案,但没有关于turtle图形的解决方案…哇,你所做的是我的最终目标。非常感谢你。我想使用海龟图形布局文本,如乳胶。