使用turtle库、time库编程实现循环交替绘制当前时间和自己学号的七段数码管,即绘制七段数码管显示当前时间(时分秒),一秒后绘制七段数码管显示自己学号,不断循环。
时间: 2024-03-01 07:53:36 浏览: 83
以下是使用turtle库和time库实现循环交替绘制当前时间和自己学号的七段数码管的代码:
```python
import turtle
import time
# 设置七段数码管的数字
digits = {
0: [1, 1, 1, 0, 1, 1, 1],
1: [0, 0, 1, 0, 0, 1, 0],
2: [1, 0, 1, 1, 1, 0, 1],
3: [1, 0, 1, 1, 0, 1, 1],
4: [0, 1, 1, 1, 0, 1, 0],
5: [1, 1, 0, 1, 0, 1, 1],
6: [1, 1, 0, 1, 1, 1, 1],
7: [1, 0, 1, 0, 0, 1, 0],
8: [1, 1, 1, 1, 1, 1, 1],
9: [1, 1, 1, 1, 0, 1, 1]
}
# 设置每个数码管的位置
positions = [
[-200, 200],
[-50, 200],
[100, 200],
[-200, 50],
[-50, 50],
[100, 50],
[-75, -100]
]
# 绘制一个七段数码管
def draw_digit(position, digit):
turtle.penup()
turtle.goto(position)
turtle.pendown()
if digit[0]:
turtle.forward(50)
else:
turtle.penup()
turtle.forward(50)
turtle.pendown()
turtle.right(90)
if digit[1]:
turtle.forward(50)
else:
turtle.penup()
turtle.forward(50)
turtle.pendown()
turtle.right(90)
if digit[2]:
turtle.forward(50)
else:
turtle.penup()
turtle.forward(50)
turtle.pendown()
turtle.right(45)
if digit[3]:
turtle.forward(35.4)
else:
turtle.penup()
turtle.forward(35.4)
turtle.pendown()
turtle.right(90)
if digit[4]:
turtle.forward(35.4)
else:
turtle.penup()
turtle.forward(35.4)
turtle.pendown()
turtle.right(45)
if digit[5]:
turtle.forward(50)
else:
turtle.penup()
turtle.forward(50)
turtle.pendown()
turtle.right(90)
if digit[6]:
turtle.forward(50)
else:
turtle.penup()
turtle.forward(50)
turtle.pendown()
turtle.penup()
# 绘制当前时间的七段数码管
def draw_time():
# 获取当前时间
now = time.strftime("%H%M%S")
# 绘制数字
for i in range(len(now)):
draw_digit(positions[i], digits[int(now[i])])
# 绘制学号的七段数码管
def draw_number():
number = "2018111111"
# 绘制数字
for i in range(len(number)):
draw_digit(positions[i], digits[int(number[i])])
# 设置画布大小
turtle.setup(500, 500)
# 循环绘制七段数码管
while True:
turtle.clear()
draw_time()
turtle.update()
time.sleep(1)
turtle.clear()
draw_number()
turtle.update()
time.sleep(1)
turtle.done()
```
这段代码会在窗口中交替绘制当前时间和自己的学号的七段数码管,每隔一秒钟切换一次。
阅读全文