如何让 raspberry pi 显示当前时间

how to get a raspberry pi to display the current time

如何使用“rpi_lcd”库获得一个 raspberry pi 16x2 包机显示以显示 EST 时区的当前时间,谢谢高级

非常简单,您只需要使用 datetime 并创建自定义时间 format 并将其传递给 lcd.text 函数即可。此代码假定您的电路连接正常。

import datetime
import pytz
from time import sleep
from rpi_lcd import LCD

lcd = LCD(width=16, rows=2)
tz = pytz.timezone("US/Eastern")

while True:
    lcd.text(datetime.datetime.now(tz=tz).strftime(
        "%y-%m-%d %H:%M:%S"), 1)  # for a 16x2 display
    sleep(1)
    lcd.clear()