转载请注明出处:小锋学长生活大爆炸[xfxuezhagn.cn]
如果本文帮助到了你,欢迎[点赞、收藏、关注]哦~
目录
背景说明
运行效果
参考代码
背景说明
有一个空闲的jetson和13.3寸的屏幕,闲着也是闲着,拿来显示时钟好了。浏览器显示在线时钟的方式直接占用80%的CPU,所以为了降低资源占用所以用Python写了个。
运行效果
有待添加更多内容(本来想想把魔镜加进来,但发现jetson有很多包装不上,就放弃了)。按esc按键可以退出全屏。
参考代码
import pygame import pygame.freetype import time import locale from lunarcalendar import Converter, Solar # 设置中国时间和语言环境 locale.setlocale(locale.LC_TIME, 'zh_CN.utf8') # 初始化 Pygame pygame.init() screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN) pygame.display.set_caption('Flip Clock with Date, Day, and Lunar Calendar') # 使用支持中文的字体文件 font_path = 'SimHei.ttf' # 确保将字体文件路径设置为正确的位置 date_font = pygame.freetype.Font(font_path, 50) time_font = pygame.freetype.Font(font_path, 180) lunar_font = pygame.freetype.Font(font_path, 30) digit_font = pygame.freetype.Font(font_path, 180) # 颜色和布局参数 background_color = (0, 0, 0) digit_color = (200, 200, 200) frame_color = (50, 50, 50) frame_padding = 10 width, height = screen.get_width(), screen.get_height() digit_width, digit_height = 150, 200 margin = 20 x_start = (width - 8 * digit_width - 7 * margin) // 2 y_start = (height - digit_height) // 2 + 50 # 初始化时钟 clock = pygame.time.Clock() rendered_text_cache = {} def draw_flip_clock(screen, current_time, current_date, lunar_date): screen.fill(background_color) # 渲染公历日期和星期几 date_surface, date_rect = get_rendered_text(date_font, current_date, (255, 255, 255)) date_rect.center = (screen.get_width() // 2, screen.get_height() // 4 - 50) screen.blit(date_surface, date_rect) # 渲染农历日期 lunar_surface, lunar_rect = get_rendered_text(lunar_font, lunar_date, (255, 255, 255)) lunar_rect.center = (screen.get_width() // 2, screen.get_height() // 4) screen.blit(lunar_surface, lunar_rect) # 渲染翻页时钟 draw_flip_numbers(screen, current_time) def draw_flip_numbers(screen, current_time): for i, char in enumerate(current_time): if char.isdigit(): # 绘制背景框 rect_x = x_start + i * (digit_width + margin) rect_y = y_start - frame_padding pygame.draw.rect(screen, frame_color, (rect_x, rect_y, digit_width, digit_height + 2 * frame_padding), border_radius=10) # 绘制数字 digit_surface, digit_rect = get_rendered_text(digit_font, char, digit_color) digit_rect.center = (rect_x + digit_width // 2, y_start + digit_height // 2) screen.blit(digit_surface, digit_rect) else: # 绘制冒号,不加背景框 colon_surface, colon_rect = get_rendered_text(digit_font, char, digit_color) colon_rect.center = (x_start + i * (digit_width + margin) + digit_width // 2, y_start + digit_height // 2) screen.blit(colon_surface, colon_rect) def get_rendered_text(font, text, color): if text not in rendered_text_cache: rendered_text_cache[text] = font.render(text, color) return rendered_text_cache[text] def get_time_strings(): now = time.localtime() current_time = time.strftime('%H:%M:%S', now) current_date = time.strftime('%Y-%m-%d %A', now) solar = Solar(now.tm_year, now.tm_mon, now.tm_mday) lunar = Converter.Solar2Lunar(solar) lunar_date = f"农历 {lunar.year}年{lunar.month}月{lunar.day}日" return current_time, current_date, lunar_date def main(): running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: running = False current_time, current_date, lunar_date = get_time_strings() draw_flip_clock(screen, current_time, current_date, lunar_date) pygame.display.flip() time.sleep(0.5) pygame.quit() if __name__ == '__main__': main()
还没有评论,来说两句吧...