用python3实现数码管绘制程序。 自行设计界面上的输出信息"加上班级 姓名 学号,日期的年月日等" 包括字体、颜色 、样式 均可自行编辑.
时间: 2023-08-29 09:12:29 浏览: 78
以下是一个简单的数码管绘制程序示例,使用 Python 3 和 Pygame 库实现:
```python
import pygame
import time
import datetime
# 设置窗口大小
WIDTH, HEIGHT = 800, 600
# 设置数码管颜色和大小
COLOR_ON = (255, 0, 0)
COLOR_OFF = (50, 50, 50)
SEG_SIZE = 20
SEG_GAP = 4
# 数码管数字编码
DIGITS = {
0: [1, 1, 1, 1, 1, 1, 0],
1: [0, 1, 1, 0, 0, 0, 0],
2: [1, 1, 0, 1, 1, 0, 1],
3: [1, 1, 1, 1, 0, 0, 1],
4: [0, 1, 1, 0, 0, 1, 1],
5: [1, 0, 1, 1, 0, 1, 1],
6: [1, 0, 1, 1, 1, 1, 1],
7: [1, 1, 1, 0, 0, 0, 0],
8: [1, 1, 1, 1, 1, 1, 1],
9: [1, 1, 1, 1, 0, 1, 1]
}
# 初始化 Pygame
pygame.init()
# 创建窗口
screen = pygame.display.set_mode((WIDTH, HEIGHT))
# 设置窗口标题
pygame.display.set_caption("数码管")
# 设置字体和字体大小
font = pygame.font.SysFont('arial', 36)
# 获取当前时间
now = datetime.datetime.now()
# 循环绘制数字
while True:
# 处理 Pygame 事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
# 填充背景色
screen.fill((0, 0, 0))
# 获取当前时间
now = datetime.datetime.now()
# 绘制班级、姓名、学号、日期等信息
text1 = font.render("班级:XXXXXX", True, (255, 255, 255))
text2 = font.render("姓名:XXXXXX", True, (255, 255, 255))
text3 = font.render("学号:XXXXXXXXXX", True, (255, 255, 255))
text4 = font.render("日期:{}".format(now.strftime("%Y-%m-%d")), True, (255, 255, 255))
screen.blit(text1, (50, 50))
screen.blit(text2, (50, 100))
screen.blit(text3, (50, 150))
screen.blit(text4, (50, 200))
# 获取当前时间的小时、分钟、秒钟
hour, minute, second = now.hour, now.minute, now.second
# 计算数码管位置和大小
x = 400
y = 300
seg_width = SEG_SIZE * 3 + SEG_GAP * 2
seg_height = SEG_SIZE * 5 + SEG_GAP * 4
# 绘制小时数码管
digits = [int(d) for d in str(hour).zfill(2)]
for i, digit in enumerate(digits):
if DIGITS[digit][0]:
pygame.draw.rect(screen, COLOR_ON, (x + i * (SEG_SIZE + SEG_GAP), y, SEG_SIZE, SEG_SIZE))
else:
pygame.draw.rect(screen, COLOR_OFF, (x + i * (SEG_SIZE + SEG_GAP), y, SEG_SIZE, SEG_SIZE))
if DIGITS[digit][1]:
pygame.draw.rect(screen, COLOR_ON, (x + i * (SEG_SIZE + SEG_GAP), y + SEG_SIZE + SEG_GAP, SEG_SIZE, SEG_SIZE))
else:
pygame.draw.rect(screen, COLOR_OFF, (x + i * (SEG_SIZE + SEG_GAP), y + SEG_SIZE + SEG_GAP, SEG_SIZE, SEG_SIZE))
if DIGITS[digit][2]:
pygame.draw.rect(screen, COLOR_ON, (x + i * (SEG_SIZE + SEG_GAP), y + (SEG_SIZE + SEG_GAP) * 2, SEG_SIZE, SEG_SIZE))
else:
pygame.draw.rect(screen, COLOR_OFF, (x + i * (SEG_SIZE + SEG_GAP), y + (SEG_SIZE + SEG_GAP) * 2, SEG_SIZE, SEG_SIZE))
if DIGITS[digit][3]:
pygame.draw.rect(screen, COLOR_ON, (x + i * (SEG_SIZE + SEG_GAP), y + (SEG_SIZE + SEG_GAP) * 3, SEG_SIZE, SEG_SIZE))
else:
pygame.draw.rect(screen, COLOR_OFF, (x + i * (SEG_SIZE + SEG_GAP), y + (SEG_SIZE + SEG_GAP) * 3, SEG_SIZE, SEG_SIZE))
if DIGITS[digit][4]:
pygame.draw.rect(screen, COLOR_ON, (x + i * (SEG_SIZE + SEG_GAP), y + (SEG_SIZE + SEG_GAP) * 4, SEG_SIZE, SEG_SIZE))
else:
pygame.draw.rect(screen, COLOR_OFF, (x + i * (SEG_SIZE + SEG_GAP), y + (SEG_SIZE + SEG_GAP) * 4, SEG_SIZE, SEG_SIZE))
# 绘制冒号
pygame.draw.rect(screen, COLOR_ON, (x + seg_width, y + SEG_SIZE + SEG_GAP, SEG_SIZE, SEG_SIZE))
pygame.draw.rect(screen, COLOR_ON, (x + seg_width, y + (SEG_SIZE + SEG_GAP) * 3, SEG_SIZE, SEG_SIZE))
# 绘制分钟数码管
digits = [int(d) for d in str(minute).zfill(2)]
for i, digit in enumerate(digits):
if DIGITS[digit][0]:
pygame.draw.rect(screen, COLOR_ON, (x + seg_width + SEG_SIZE + SEG_GAP + i * (SEG_SIZE + SEG_GAP), y, SEG_SIZE, SEG_SIZE))
else:
pygame.draw.rect(screen, COLOR_OFF, (x + seg_width + SEG_SIZE + SEG_GAP + i * (SEG_SIZE + SEG_GAP), y, SEG_SIZE, SEG_SIZE))
if DIGITS[digit][1]:
pygame.draw.rect(screen, COLOR_ON, (x + seg_width + SEG_SIZE + SEG_GAP + i * (SEG_SIZE + SEG_GAP), y + SEG_SIZE + SEG_GAP, SEG_SIZE, SEG_SIZE))
else:
pygame.draw.rect(screen, COLOR_OFF, (x + seg_width + SEG_SIZE + SEG_GAP + i * (SEG_SIZE + SEG_GAP), y + SEG_SIZE + SEG_GAP, SEG_SIZE, SEG_SIZE))
if DIGITS[digit][2]:
pygame.draw.rect(screen, COLOR_ON, (x + seg_width + SEG_SIZE + SEG_GAP + i * (SEG_SIZE + SEG_GAP), y + (SEG_SIZE + SEG_GAP) * 2, SEG_SIZE, SEG_SIZE))
else:
pygame.draw.rect(screen, COLOR_OFF, (x + seg_width + SEG_SIZE + SEG_GAP + i * (SEG_SIZE + SEG_GAP), y + (SEG_SIZE + SEG_GAP) * 2, SEG_SIZE, SEG_SIZE))
if DIGITS[digit][3]:
pygame.draw.rect(screen, COLOR_ON, (x + seg_width + SEG_SIZE + SEG_GAP + i * (SEG_SIZE + SEG_GAP), y + (SEG_SIZE + SEG_GAP) * 3, SEG_SIZE, SEG_SIZE))
else:
pygame.draw.rect(screen, COLOR_OFF, (x + seg_width + SEG_SIZE + SEG_GAP + i * (SEG_SIZE + SEG_GAP), y + (SEG_SIZE + SEG_GAP) * 3, SEG_SIZE, SEG_SIZE))
if DIGITS[digit][4]:
pygame.draw.rect(screen, COLOR_ON, (x + seg_width + SEG_SIZE + SEG_GAP + i * (SEG_SIZE + SEG_GAP), y + (SEG_SIZE + SEG_GAP) * 4, SEG_SIZE, SEG_SIZE))
else:
pygame.draw.rect(screen, COLOR_OFF, (x + seg_width + SEG_SIZE + SEG_GAP + i * (SEG_SIZE + SEG_GAP), y + (SEG_SIZE + SEG_GAP) * 4, SEG_SIZE, SEG_SIZE))
# 绘制秒钟数码管
digits = [int(d) for d in str(second).zfill(2)]
for i, digit in enumerate(digits):
if DIGITS[digit][0]:
pygame.draw.rect(screen, COLOR_ON, (x + seg_width * 2 + SEG_SIZE * 2 + SEG_GAP * 2 + i * (SEG_SIZE + SEG_GAP), y, SEG_SIZE, SEG_SIZE))
else:
pygame.draw.rect(screen, COLOR_OFF, (x + seg_width * 2 + SEG_SIZE * 2 + SEG_GAP * 2 + i * (SEG_SIZE + SEG_GAP), y, SEG_SIZE, SEG_SIZE))
if DIGITS[digit][1]:
pygame.draw.rect(screen, COLOR_ON, (x + seg_width * 2 + SEG_SIZE * 2 + SEG_GAP * 2 + i * (SEG_SIZE + SEG_GAP), y + SEG_SIZE + SEG_GAP, SEG_SIZE, SEG_SIZE))
else:
pygame.draw.rect(screen, COLOR_OFF, (x + seg_width * 2 + SEG_SIZE * 2 + SEG_GAP * 2 + i * (SEG_SIZE + SEG_GAP), y + SEG_SIZE + SEG_GAP, SEG_SIZE, SEG_SIZE))
if DIGITS[digit][2]:
pygame.draw.rect(screen, COLOR_ON, (x + seg_width * 2 + SEG_SIZE * 2 + SEG_GAP * 2 + i * (SEG_SIZE + SEG_GAP), y + (SEG_SIZE + SEG_GAP) * 2, SEG_SIZE, SEG_SIZE))
else:
pygame.draw.rect(screen, COLOR_OFF, (x + seg_width * 2 + SEG_SIZE * 2 + SEG_GAP * 2 + i * (SEG_SIZE + SEG_GAP), y + (SEG_SIZE + SEG_GAP) * 2, SEG_SIZE, SEG_SIZE))
if DIGITS[digit][3]:
pygame.draw.rect(screen, COLOR_ON, (x + seg_width * 2 + SEG_SIZE * 2 + SEG_GAP * 2 + i * (SEG_SIZE + SEG_GAP), y + (SEG_SIZE + SEG_GAP) * 3, SEG_SIZE, SEG_SIZE))
else:
pygame.draw.rect(screen, COLOR_OFF, (x + seg_width * 2 + SEG_SIZE * 2 + SEG_GAP * 2 + i * (SEG_SIZE + SEG_GAP), y + (SEG_SIZE + SEG_GAP) * 3, SEG_SIZE, SEG_SIZE))
if DIGITS[digit][4]:
pygame.draw.rect(screen, COLOR_ON, (x + seg_width * 2 + SEG_SIZE * 2 + SEG_GAP * 2 + i * (SEG_SIZE + SEG_GAP), y + (SEG_SIZE + SEG_GAP) * 4, SEG_SIZE, SEG_SIZE))
else:
pygame.draw.rect(screen, COLOR_OFF, (x + seg_width * 2 + SEG_SIZE * 2 + SEG_GAP * 2 + i * (SEG_SIZE + SEG_GAP), y + (SEG_SIZE + SEG_GAP) * 4, SEG_SIZE, SEG_SIZE))
# 刷新屏幕
pygame.display.flip()
# 等待1秒
time.sleep(1)
```
这个程序会在屏幕上绘制一个数码管,显示当前时间,并在屏幕上显示班级、姓名、学号、日期等信息。你可以根据需要修改这些信息的字体、颜色和样式。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)