gpt4 book ai didi

python - 在Python中的圆圈内插入文本

转载 作者:太空宇宙 更新时间:2023-11-03 15:54:33 24 4
gpt4 key购买 nike

我正在尝试编写用于绘制 DFA 的 Python 代码。我打算使用 turtle 库。有替代方案吗?我可以绘制节点,但不确定如何在圆圈内插入州名称。有人可以指导我吗?下面是到目前为止我的代码。谢谢!

import turtle

def draw_node(some_turtle):
myTurtle.circle(50)
turtle.getscreen().__root.mainloop()


def draw_design():

window = turtle.Screen()
window.bgcolor("teal")

myTurtle = turtle.Turtle()
myTurtle.color("white")
myTurtle.shape("turtle")
myTurtle.speed(5)
myTurtle.pensize(4)

draw_node(myTurtle)

window.exitonclick()

draw_design()

最佳答案

您的代码问题似乎是由于对 turtle 库特别不熟悉以及对Python编程不熟悉而造成的。我不一定说 turtle 库是您想要做的事情的最佳选择,但它可以完成您的程序渴望实现的目标:

from turtle import Turtle, Screen

RADIUS = 50

FONT_SIZE = 18

FONT = ("Arial", FONT_SIZE, "normal")

def draw_node(turtle, text, x, y):
turtle.up()
turtle.goto(x, y - RADIUS)
turtle.down()
turtle.circle(RADIUS)
turtle.up()
turtle.goto(x, y - FONT_SIZE // 2)
turtle.write(text, align="center", font=FONT)

def draw_design(turtle):

turtle.color("white")
turtle.pensize(4)

draw_node(turtle, "S0", -100, 100)

draw_node(turtle, "S1", 100, 100)

screen = Screen()
screen.bgcolor("blue")

yertle = Turtle(shape="turtle")

draw_design(yertle)

yertle.home()

screen.exitonclick()

输出

enter image description here

关于python - 在Python中的圆圈内插入文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40925048/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com