python - 如何在 Python 中的框前显示文本?
问题描述
我创建了一个框和一个文本,我希望文本显示在框的前面。但是当我尝试这样做时,我看不到它,因为它在盒子后面。
import turtle
wn= turtle.Screen()
style = ("Courier", "36", "bold")
#Box -------------------------- ([Text] Box)
b1 = turtle.Turtle()
b1.color("black")
b1.shape("square")
b1.speed(0)
b1.shapesize(stretch_wid=5, stretch_len=10)
b1.penup()
b1.goto(-400, -150)
#Text ------------------------ "[Text]"
t1= turtle.Turtle()
t1.speed(0)
t1.color("white")
t1.ht()
t1.penup()
t1.goto(-400, -150)
t1.write("[Text]", font=style, align = "center")
#Main loop
while True:
wn.update()
我检查了代码的顺序是否错误,但没有发现任何错误。我也试过删除t1.ht()
,但这不是问题。我如何解决它?
解决方案
下面是一种混合了您尝试做的和@furas 推荐的方法。它使用冲压而不是绘图来简化问题:
from turtle import Screen, Turtle
FONT_SIZE = 36
FONT = ('Courier', FONT_SIZE, 'bold')
screen = Screen()
textbox = Turtle()
textbox.hideturtle()
textbox.color('black')
textbox.shape('square')
textbox.shapesize(stretch_wid=5, stretch_len=10)
textbox.penup()
textbox.goto(-300, -150)
textbox.stamp()
textbox.color('white')
textbox.goto(-300, -150 - FONT_SIZE/2) # center vertically based on font size
textbox.write("[Text]", align='center', font=FONT)
screen.mainloop()
textbox
乌龟也可以重复使用和重塑以绘制其他文本框。通过一些思考和猜测,您可以根据字体大小和文本本身计算出合理的文本框大小。
推荐阅读
- c - STM32相同的while循环代码但编译成不同的汇编代码
- angular - Angular @Input 将数字值从父组件传递到子组件
- google-apps-script - 是否有 Classroom.Courses.Topics.get 的示例,以便我可以使用现有 topicId 调用 Classroom.Courses.CourseWork.create?
- laravel - 如何在 Laravel 中建立这种 Eloquent 关系
- html - HTML img 自动旋转
- json - overpass-turbo.eu 在地图上找到所有城市
- oauth-2.0 - 将 Google OIDC 与代码流和 PKCE 结合使用
- jenkins - 詹金斯从半点开始计时
- html - 我如何在引导表中删除此边框
- spring-boot - 嵌入式 Spring Boot 配置文件在命令行上覆盖配置文件后留下元素