Python编写时钟程序(运行结果在“视频“选项里)(适合学过编程2年00个月~2年11个月的人(中级篇))

1.初始设置

from datetime import*#导入相关库

from turtle import*

from time import sleep

setup(675,675)#设置窗口大小

hideturtle()#隐藏海龟

mode("logo")#设置模式

2.读取时间

while(True):#重复执行

        hideturtle()#隐藏海龟

        curr=datetime.now()#读取时间

        year=curr.year

        month=curr.month

        day=curr.day

        hour=curr.hour

        minute=curr.minute

        second=curr.second

3.数据加工

        if month<10:#不满10,在前面加个0

            showmonth="0"+str(month)

        else:

            showmonth=str(month)

        if day<10:

            showday="0"+str(day)

        else:

            showday=str(day)

        if hour<10:

            showhour="0"+str(hour)

        else:

            showhour=str(hour)

        if minute<10:

            showminute="0"+str(minute)

        else:

            showminute=str(minute)

        if second<10:

            showsecond="0"+str(second)

        else:

            showsecond=str(second)

        weekday=(curr.weekday())#读取星期

        weekday+=1#(实际是0~6,要改成1~7)

4.显示时间

        show="(星期"+str(weekday)+")"+str(year)+"年"+showmonth+"月"+showday+"日"+showhour+"时"+showminute+"分"+showsecond+"秒"#设时间

        write(show,font=("Times New Roman",15))#写时间(同时设置字体)

        sleep(.9512)#打印需要一点点时间,所以等待时间要比1秒少一点点

5.完整代码

from datetime import*#导入相关库

from turtle import*

from time import sleep

setup(675,675)#设置窗口大小

hideturtle()#隐藏海龟

mode("logo")#设置模式

while(True):#重复执行

        hideturtle()#隐藏海龟

        curr=datetime.now()#读取时间

        year=curr.year

        month=curr.month

        day=curr.day

        hour=curr.hour

        minute=curr.minute

        second=curr.second

        if month<10:#不满10,在前面加个0

            showmonth="0"+str(month)

        else:

            showmonth=str(month)

        if day<10:

            showday="0"+str(day)

        else:

            showday=str(day)

        if hour<10:

            showhour="0"+str(hour)

        else:

            showhour=str(hour)

        if minute<10:

            showminute="0"+str(minute)

        else:

            showminute=str(minute)

        if second<10:

            showsecond="0"+str(second)

        else:

            showsecond=str(second)

        weekday=(curr.weekday())#读取星期

        weekday+=1#(实际是0~6,要改成1~7)

        show="(星期"+str(weekday)+")"+str(year)+"年"+showmonth+"月"+showday+"日"+showhour+"时"+showminute+"分"+showsecond+"秒"#设时间

        write(show,font=("Times New Roman",15))#写时间(同时设置字体)

        sleep(.9512)#打印需要一点点时间,所以等待时间要比1秒少一点点

你可能感兴趣的:(python)