1. (程序题) 请用python完成一个动态时钟的制作。提示:用turtle画时钟,通过第三方库datetime导入时间值。
时间: 2024-10-09 19:12:09 浏览: 37
python画图-使用Python+turtle实现画时钟.zip
当然可以。你可以使用Python的`turtle`库来创建一个动态时钟。首先,你需要安装`turtle`模块,如果尚未安装,可以在命令行输入`pip install turtle`。下面是一个简单的示例,它会每秒更新当前的时间:
```python
import turtle
import datetime
# 初始化Turtle对象并隐藏画笔
clock_turtle = turtle.Turtle()
clock_turtle.hideturtle()
# 设置背景颜色和时钟字体
bg_color = "black"
text_color = "white"
clock_turtle.penup()
clock_turtle.goto(-40, -50)
clock_turtle.pendown()
clock_turtle.fillcolor(bg_color)
clock_turtle.begin_fill()
clock_turtle.circle(80)
clock_turtle.end_fill()
def draw_time():
current_time = datetime.datetime.now().strftime("%H:%M:%S")
x_position = 150
y_position = -60
for part in current_time:
if part == ":":
y_position -= 30
clock_turtle.penup()
clock_turtle.goto(x_position, y_position)
clock_turtle.pendown()
clock_turtle.write(part, font=("Arial", 20, "normal"))
else:
clock_turtle.penup()
clock_turtle.goto(x_position, y_position)
clock_turtle.pendown()
clock_turtle.write(part, font=("Arial", 40, "bold"))
x_position -= 70
# 每秒更新一次时钟
while True:
draw_time()
turtle.done() # 这句代码让窗口保持打开直到手动关闭
```
运行这段代码后,你会看到一个动态的时钟显示在屏幕上。
阅读全文