今天看到别的大佬用Python写时钟
我密集恐惧症都犯了
十来个定义函数 我看也看不懂
代码还有整整78行 还是在我改良后的
改良前有98行 我删注释 优化函数名(比如把penup改成pu)
这个文件照样有5KB
并且在帧数取决于电脑性能 我运行了一下 电脑都卡出蓝屏了
于是我连夜赶工 把这个6行的时钟搞出来了
在运行十分钟后也只有三十几MB
文件大小也只有723B
_____________________________________________________________________________
总之 先放代码:
import turtle,time,datetime;date=datetime.datetime.now();turtle.tracer(0);mz=turtle.Turtle();fz=turtle.Turtle();fz.pensize(3);sz=turtle.Turtle();sz.pensize(5);bg=turtle.Turtle();bg.ht();bg.pu();bg.goto(0,-200)
for i in [6,5,4,3,2,1,12,11,10,9,8,7]:bg.write(i,font=('斜体',20,'normal'));bg.circle(200,30)
bg.goto(0,-210);bg.pd();bg.pensize(5);bg.circle(220)
def zd(pen,ds,cd):pen.clear();pen.pu();pen.goto(0,0);pen.pd();pen.right(ds);pen.fd(cd)
zd(mz,int(date.strftime('%S'))*6-90,180);zd(fz,int(date.strftime('%M'))*6-90,140);zd(sz,int(date.strftime('%H'))*30-90,100)
while 1:time.sleep(0.1);zd(mz,0.6,180);zd(fz,0.01,140);zd(sz,0.025/30,100);turtle.update()
未登录的复制渠道:
import turtle,time,datetime;date=datetime.datetime.now();turtle.tracer(0);mz=turtle.Turtle();fz=turtle.Turtle();fz.pensize(3);sz=turtle.Turtle();sz.pensize(5);bg=turtle.Turtle();bg.ht();bg.pu();bg.goto(0,-200)
for i in [6,5,4,3,2,1,12,11,10,9,8,7]:bg.write(i,font=('斜体',20,'normal'));bg.circle(200,30)
bg.goto(0,-210);bg.pd();bg.pensize(5);bg.circle(220)
def zd(pen,ds,cd):pen.clear();pen.pu();pen.goto(0,0);pen.pd();pen.right(ds);pen.fd(cd)
zd(mz,int(date.strftime('%S'))*6-90,180);zd(fz,int(date.strftime('%M'))*6-90,140);zd(sz,int(date.strftime('%H'))*30-90,100)
while 1:time.sleep(0.1);zd(mz,0.6,180);zd(fz,0.01,140);zd(sz,0.025/30,100);turtle.update()
_____________________________________________________________________________
现在是北京时间10:40
我运行一下程序 是下图的效果:
_____________________________________________________________________________
下面是步骤
1.基础设置:
import datetime#导入datetime模块 负责获得当前时间
import turlte#导入turtle模块 负责图像显示
import time#导入time模块 负责调节帧率和减小电脑压力
date=datetime.datetime.now()#初始化datetime
turtle.tracer(0)#以最快速度运行
_____________________________________________________________________________
2.指针设置:
mz=turtle.Turtle()#创建秒针
fz=turtle.Turtle()#创建分针
fz.pensize(3)#把分针的宽度改为3像素
sz=turtle.Turtle()#创建时针
sz.pensize(5)#把时针的宽度改为5像素
_____________________________________________________________________________
3.表盘绘制:
bg=turtle.Turtle()#创建画笔bg
bg.ht()#隐藏画笔bg
bg.pu()#不让画笔bg留下痕迹
bg.goto(0,-200)#让画笔bg去0,-200的位置 这可以让圆心正好是0,0
for i in [6,5,4,3,2,1,12,11,10,9,8,7]:#让for循环遍历这个列表bg.write(i,font=('斜体',20,'normal'))#写出的字为当前列表中对应的数字bg.circle(200,30)#画一个圆的十二分之一
bg.goto(0,-210)#去圆外10像素的地方
bg.pd()#让画笔留下痕迹
bg.pensize(5)#把画笔bg的宽度改为5像素
bg.circle(220)#画一个半径为220的圆
_____________________________________________________________________________
4.转动函数:
def zd(pen,ds,cd):#定义函数zd("转动"的首字母)pen.clear()#清除这支画笔在这之前留下的痕迹pen.pu()#不让画笔留下痕迹pen.goto(0,0)#让画笔去中心pen.pd()#让画笔留下痕迹pen.right(ds)#让这只画笔右转(顺时针转)动ds度pen.fd(cd)#向前cd像素
_____________________________________________________________________________
5.转动:
zd(mz,int(date.strftime('%S'))*6-90,180)#获得秒
zd(fz,int(date.strftime('%M'))*6-90,140)#获得分
zd(sz,int(date.strftime('%H'))*30-90,100)#获得时
'''
date是之前初始化的datetime
strftime函数的参数里 特定的口令代表获得的时间(是字符串类型)
'''
while 1:time.sleep(0.1)#停止运行0.1秒(调节帧率 把一秒分成10帧)zd(mz,0.6,180)#秒针转动0.6° 长180像素zd(fz,0.01,140)#分针转动0.01° 长140像素zd(sz,0.025/30,100)#时针转动0.025/30°=0.3/360turtle.update()#刷新画面
#以下是调帧率的代码 变量a就是帧率 要自己填
'''
while 1:time.sleep(1/a)zd(mz,6/a,180)zd(fz,0.1/a,140)zd(sz,0.025/3/a)turtle.update()
'''
_____________________________________________________________________________
总览:
import turtle,time,datetime
date=datetime.datetime.now()
turtle.tracer(0)
mz=turtle.Turtle()
fz=turtle.Turtle()
fz.pensize(3)
sz=turtle.Turtle()
sz.pensize(5)
bg=turtle.Turtle()
bg.ht()
bg.pu()
bg.goto(0,-200)
for i in [6,5,4,3,2,1,12,11,10,9,8,7]:bg.write(i,font=('斜体',20,'normal'))bg.circle(200,30)
bg.goto(0,-210)
bg.pd()
bg.pensize(5)
bg.circle(220)
def zd(pen,ds,cd):pen.clear()pen.pu()pen.goto(0,0)pen.pd()pen.right(ds)pen.fd(cd)
zd(mz,int(date.strftime('%S'))*6-90,180)
zd(fz,int(date.strftime('%M'))*6-90,140)
zd(sz,int(date.strftime('%H'))*30-90,100)
while 1:time.sleep(0.1)zd(mz,0.6,180)zd(fz,0.01,140)zd(sz,0.025/30,100)turtle.update()
未登录的复制渠道:
import turtle,time,datetime
date=datetime.datetime.now()
turtle.tracer(0)
mz=turtle.Turtle()
fz=turtle.Turtle()
fz.pensize(3)
sz=turtle.Turtle()
sz.pensize(5)
bg=turtle.Turtle()
bg.ht()
bg.pu()
bg.goto(0,-200)
for i in [6,5,4,3,2,1,12,11,10,9,8,7]:
bg.write(i,font=('斜体',20,'normal'))
bg.circle(200,30)
bg.goto(0,-210)
bg.pd()
bg.pensize(5)
bg.circle(220)
def zd(pen,ds,cd):
pen.clear()
pen.pu()
pen.goto(0,0)
pen.pd()
pen.right(ds)
pen.fd(cd)
zd(mz,int(date.strftime('%S'))*6-90,180)
zd(fz,int(date.strftime('%M'))*6-90,140)
zd(sz,int(date.strftime('%H'))*30-90,100)
while 1:
time.sleep(0.1)
zd(mz,0.6,180)
zd(fz,0.01,140)
zd(sz,0.025/30,100)
turtle.update()
_____________________________________________________________________________
MVP结算:
本期字数:4763
本期质量分:85
15赞Python 30赞c++ 10赞花絮