精华内容
下载资源
问答
  • Python钟表代码里的

    2021-06-23 13:38:24
    这个负号是什么意思啊被老师问懵了
  • 演示7:python钟表绘制

    2021-07-09 14:04:07
    演示7:python钟表绘制介绍基础功能:万年历月份功能:万年历日期功能:万年历星期功能:样品展示知识源码 介绍 为学生演示而用,使用tkinter控件库实现。 其中使用time库控制绘制帧数,使用datetime获取相关时间...

    介绍

    为学生演示而用,使用tkinter控件库实现。
    其中使用time库控制绘制帧数,使用datetime获取相关时间数据,使用_thread来调用绘制函数。

    基础功能:

    1. 秒针,每0.1秒跳动一次,每跳动一次旋转0.6度;
    2. 分针,每1秒跳动一次,每跳动一次旋转0.1度 ;
    3. 时针,每1秒跳动一次,每跳动一次旋转约0.0083度;

    万年历月份功能:

    1. 指向当前月份的数字;

    万年历日期功能:

    1. 指向当前日期的数字,部分数字用点代替;

    万年历星期功能:

    1. 指向当前星期的英文缩写;

    样品展示

    模仿江诗丹顿的表盘

    知识

    1. Canvas 画布,属于tkinter库,可以在上面绘制各类图形
      tk,必要填入的窗口变量,表示该画布是要绘制在某个控件上面;
      width,必要填写的画布宽度;
      height,必要填写的画布敢赌;
    2. place,设置画布左上角的显示位置,其他控件通用;
    3. create_oval,绘制圆
      (x1,y1,x2,y2),x1与y1是圆所属的矩形左上角坐标,x2与y2是圆所属的矩形右下角坐标
      width,圆的边宽度
      outline,圆的边颜色
      fill,圆中间填充的颜色
    4. create_polygon,绘制多边形
      (x1,y1,x2,y2,x3,y3,…),填写每一个折点的坐标
      其他同绘制圆
    5. create_text,绘制文字
      (x,y),文字绘制的位置
      font,文字的字体与文字大小

    源码

    from tkinter import *
    from time import *
    from math import *
    from datetime import datetime as da
    import _thread as th
    tk=Tk()
    #窗口尺寸
    tk.minsize(400,400)
    tk.maxsize(400,400)
    tk.title('仿江诗丹顿')
    #画布
    ca=Canvas(tk,width=400,height=400)
    ca.place(x=0,y=0)#设置画布位置
    def draw():
        ca.delete('all')#清空画布
        ca.create_oval((10,10,390,390),fill='#f9f9f9')
        ca.create_oval((20,20,380,380),width=6,outline='#777777')
        ca.create_oval((10,10,390,390),width=16,outline='#aaaaaa')
        #表盘文字
        ca.create_text((200,130),text='VACHERON CONSTANTIN',font=('Tahoma',10))
        ca.create_text((200,150),text='GENEVE',font=('Tahoma',10))
        #图标
        ca.create_polygon((200,92,210,87,190,117,200,112,210,117,190,87,200,92),fill='#ffeebb',outline='#ffeebb')
        ca.create_polygon((190,102,185,112,215,92,210,102,215,112,185,92,190,102),fill='#ffeebb',outline='#ffeebb')
        #圆点
        for i in range(60):
            if i%5!=0:
                ca.create_oval((196+cos(i*pi/30)*160,196-sin(i*pi/30)*160,204+cos(i*pi/30)*160,204-sin(i*pi/30)*160),fill='#ffeebb',outline='#ffffff')
        #长条
        for i in range(12):
            #if i%3!=0:
                ca.create_polygon((200+cos(i*pi/6-0.009)*170,200-sin(i*pi/6-0.009)*170,200+cos(i*pi/6+0.009)*170,200-sin(i*pi/6+0.009)*170,\
                    200+cos(i*pi/6+0.009)*135,200-sin(i*pi/6+0.009)*135,200+cos(i*pi/6-0.009)*135,200-sin(i*pi/6-0.009)*135),fill='#ffeebb',outline='#ffeebb')
        #12点图案
        ca.create_polygon((200,28,205,33,200,36,195,33,200,28),fill='#ffeebb')
        ca.create_polygon((200,36,205,33,203,69,200,70,200,36),fill='#f1e0ad')
        ca.create_polygon((200,36,195,33,197,69,200,70,200,36),fill='#eadaaa')
        #6点图案
        ca.create_polygon((200,372,205,367,200,364,195,367,200,372),fill='#ffeebb')
        ca.create_polygon((200,364,205,367,203,331,200,330,200,364),fill='#eadaaa')
        ca.create_polygon((200,364,195,367,197,331,200,330,200,364),fill='#f1e0ad')
        #9点图案
        ca.create_polygon((28,200,33,205,36,200,33,195,28,200),fill='#ffeebb')
        ca.create_polygon((36,200,33,205,69,203,70,200,36,200),fill='#f1e0ad')
        ca.create_polygon((36,200,33,195,69,197,70,200,36,200),fill='#eadaaa')
        #3点图案
        ca.create_polygon((372,200,367,205,364,200,367,195,372,200),fill='#ffeebb')
        ca.create_polygon((364,200,367,205,331,203,330,200,364,200),fill='#eadaaa')
        ca.create_polygon((364,200,367,195,331,197,330,200,364,200),fill='#f1e0ad')
        #中心圈
        ca.create_oval((188,188,212,212),fill='#eedd99',outline='#eedd99')
        #星期
        ca.create_oval((160,230,240,310),outline='#eeeeee',fill='#fefefe')
        ca.create_text((200+cos(pi/3.5*2)*30,270-sin(pi/3.5*2)*30),text='SUN',font=('Tahoma',6),fill='#888888')
        ca.create_text((200+cos(pi/3.5)*30,270-sin(pi/3.5)*30),text='MON',font=('Tahoma',6),fill='#888888')
        ca.create_text((200+cos(pi/3.5*7)*30,270-sin(pi/3.5*7)*30),text='TUE',font=('Tahoma',6),fill='#888888')
        ca.create_text((200+cos(pi/3.5*6)*30,270-sin(pi/3.5*6)*30),text='WED',font=('Tahoma',6),fill='#888888')
        ca.create_text((200+cos(pi/3.5*5)*30,270-sin(pi/3.5*5)*30),text='THU',font=('Tahoma',6),fill='#888888')
        ca.create_text((200+cos(pi/3.5*4)*30,270-sin(pi/3.5*4)*30),text='FRI',font=('Tahoma',6),fill='#888888')
        ca.create_text((200+cos(pi/3.5*3)*30,270-sin(pi/3.5*3)*30),text='SAT',font=('Tahoma',6),fill='#888888')
        ca.create_oval((195,265,205,275),fill='#cccccc',outline='#cccccc')
        #月
        ca.create_oval((90,170,170,250),outline='#eeeeee',fill='#fefefe')
        for i in range(12):
            ca.create_text((130+cos(pi/6*(i+1)-pi/2)*32,210+sin(pi/6*(i+1)-pi/2)*32),text=str(i+1),font=('Tahoma',6),fill='#888888')
        ca.create_oval((125,205,135,215),fill='#cccccc',outline='#cccccc')
        #日
        ca.create_oval((230,170,310,250),outline='#eeeeee',fill='#fefefe')
        for i in range(31):
            if i%2==1 and i!=31:
                ca.create_text((270+cos(pi/15.5*(i+1)-pi/2)*33,210+sin(pi/15.5*(i+1)-pi/2)*33),text=str(i+1),font=('Tahoma',4),fill='#888888')
            else:
                ca.create_oval((269+cos(pi/15.5*(i+1)-pi/2)*32,209+sin(pi/15.5*(i+1)-pi/2)*32,\
                    271+cos(pi/15.5*(i+1)-pi/2)*32,211+sin(pi/15.5*(i+1)-pi/2)*32),outline="#888888",fill='#888888')
        ca.create_oval((265,205,275,215),fill='#cccccc',outline='#cccccc')
    def draw2():
        #时间数据
        ti=localtime(time())
        #各指针角度
        sec=(ti[5]+time()%1)*pi/30-pi/2
        minn=(ti[4]+ti[5]/60)*pi/30-pi/2
        hou=(ti[3]+(ti[4]*60+ti[5])/3600)*pi/6-pi/2
        week=(-1+da.now().weekday())*(pi/3.5)
        mon=(ti[1]-3)*(pi/6)
        day=(ti[2]-8)*(pi/15.5)
        #周(星期几)针绘制
        we=ca.create_polygon((200+cos(week)*25,270+sin(week)*25,\
            200+cos(week+0.08)*20,270+sin(week+0.08)*20,\
            200+cos(week+0.15)*6,270+sin(week+0.15)*6,\
            200+cos(week-0.15)*6,270+sin(week-0.15)*6,\
            200+cos(week-0.08)*20,270+sin(week-0.08)*20),fill='#cccccc',outline='#cccccc')
        wq=ca.create_oval((198,268,202,272),fill='#999999',outline='#999999')
        #月针绘制
        mo=ca.create_polygon((130+cos(mon)*25,210+sin(mon)*25,\
            130+cos(mon+0.08)*20,210+sin(mon+0.08)*20,\
            130+cos(mon+0.15)*6,210+sin(mon+0.15)*6,\
            130+cos(mon-0.15)*6,210+sin(mon-0.15)*6,\
            130+cos(mon-0.08)*20,210+sin(mon-0.08)*20),fill='#cccccc',outline='#cccccc')
        ww=ca.create_oval((128,208,132,212),fill='#999999',outline='#999999')
        #日针绘制
        dy=ca.create_polygon((270+cos(day)*25,210+sin(day)*25,\
            270+cos(day+0.08)*20,210+sin(day+0.08)*20,\
            270+cos(day+0.15)*6,210+sin(day+0.15)*6,\
            270+cos(day-0.15)*6,210+sin(day-0.15)*6,\
            270+cos(day-0.08)*20,210+sin(day-0.08)*20),fill='#cccccc',outline='#cccccc')
        wr=ca.create_oval((268,208,272,212),fill='#999999',outline='#999999')
        #时针绘制
        ho=ca.create_polygon((200+cos(hou)*110,200+sin(hou)*110,\
            200+cos(hou+0.04)*90,200+sin(hou+0.04)*90,\
            200+cos(hou+0.15)*10,200+sin(hou+0.15)*10,\
            200+cos(hou-0.15)*10,200+sin(hou-0.15)*10,\
            200+cos(hou-0.04)*90,200+sin(hou-0.04)*90),fill='#eedd99',outline='#eedd99')
        #分针绘制
        mi=ca.create_polygon((200+cos(minn)*110,200+sin(minn)*130,\
            200+cos(minn+0.025)*90,200+sin(minn+0.025)*110,\
            200+cos(minn+0.1)*10,200+sin(minn+0.1)*10,\
            200+cos(minn-0.1)*10,200+sin(minn-0.1)*10,\
            200+cos(minn-0.025)*90,200+sin(minn-0.025)*110),fill='#eedd99',outline='#eedd99')
        #秒针绘制
        se=ca.create_polygon((200+cos(sec)*150,200+sin(sec)*150,\
            200+cos(sec+0.003)*140,200+sin(sec+0.003)*140,\
            200+cos(sec+0.003)*(-50),200+sin(sec+0.003)*(-50),\
            200+cos(sec-0.003)*(-50),200+sin(sec-0.003)*(-50),\
            200+cos(sec-0.003)*140,200+sin(sec-0.003)*140),fill='#eedd99',outline='#eedd99')
        qw=ca.create_oval((198,198,202,202),fill='#888888',outline='#888888')
        sleep(0.1)#休眠0.1秒
        #删除各指针
        ca.delete(we)
        ca.delete(wq)
        ca.delete(se)
        ca.delete(mi)
        ca.delete(ho)
        ca.delete(qw)
        ca.delete(mo)
        ca.delete(ww)
        ca.delete(dy)
        ca.delete(wr)
        th.start_new_thread(draw2,())
    draw()
    th.start_new_thread(draw2,())
    mainloop()
    
    展开全文
  • /urs/bin/ python# _*_ coding: utf-8# !/usr/bin/env python# -*- coding:utf-8 -*-import turtleimport datetime# 移动一段距离def skip(distance):"""移动乌龟一段距离,不留痕迹:param distance: 像素:return:"...

    # !/urs/bin/ python

    # _*_ coding: utf-8

    # !/usr/bin/env python

    # -*- coding:utf-8 -*-

    import turtle

    import datetime

    # 移动一段距离

    def skip(distance):

    """

    移动乌龟一段距离,不留痕迹

    :param distance: 像素

    :return:

    """

    turtle.penup()

    turtle.forward(distance)

    turtle.pendown()

    def draw_clock():

    # 先画表盘

    # 先画点

    # 移动一段距离,画一个点,然后退回

    # 转动6°,再移动一段距离,画一个点,然后退回

    # 循环 60次

    # 让乌龟的方向默认向上

    turtle.reset()

    turtle.hideturtle()

    for i in range(60):

    skip(160)

    # 根据 5格一个时钟

    if i % 5 == 0:

    turtle.pensize(7)

    # 画时钟

    turtle.forward(20)

    if i == 0:

    turtle.write(12, align='center', font=('Courier', 14, 'bold'))

    elif i == 25 or i == 30 or i == 35:

    skip(25)

    turtle.write(int(i / 5), align='center', font=('Courier', 14, 'bold'))

    skip(-25)

    else:

    turtle.write(int(i / 5), align='center', font=('Courier', 14, 'bold'))

    skip(-20)

    else:

    turtle.pensize(1)

    turtle.dot()

    skip(-160)

    turtle.right(6)

    def get_week(t):

    week = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日']

    return week[t.weekday()]

    def create_hand(length, name):

    turtle.reset()

    skip(-length * 0.1)

    turtle.begin_poly()

    turtle.forward(length * 1.1)

    turtle.end_poly()

    # 注册

    turtle.register_shape(name, turtle.get_poly())

    hand = turtle.Turtle()

    hand.shape(name)

    hand.shapesize(1, 1, 3)

    return hand

    def run():

    # 不停的获取时间

    t = datetime.datetime.today()

    bob.forward(65)

    bob.write(get_week(t), align='center', font=('Courier', 14, 'bold'))

    bob.back(130)

    bob.write(t.strftime('%Y-%m-%d'), align='center', font=('Courier', 14, 'bold'))

    bob.home()

    # 指针移动

    second = t.second + t.microsecond * 0.000001

    minute = t.minute + second / 60

    hour = t.hour + minute / 60

    turtle.tracer(True)

    second_hand.setheading(6 * second)

    minute_hand.setheading(6 * minute)

    hour_hand.setheading(30 * hour)

    turtle.ontimer(run, 200)

    if __name__ == '__main__':

    # 画秒针,分针,时针

    turtle.mode('logo')

    turtle.hideturtle()

    global second_hand, minute_hand, hour_hand, bob

    second_hand = create_hand(135, 'second_hand')

    minute_hand = create_hand(125, 'minute_hand')

    hour_hand = create_hand(90, 'hour_hand')

    # 创建一个新的turtle对象,去循环的操作

    bob = turtle.Turtle()

    bob.hideturtle()

    bob.penup()

    turtle.tracer(False)

    draw_clock()

    run()

    turtle.mainloop()

    展开全文
  • 使用python画出时钟效果2020-06-23 15:00阅读数 14python生成的时钟效果,大家可以参考:使用的python版本:3.7.0 ,以下为源代码:# time.py from turtle import * from datetime import * import time ...

    使用python画出时钟效果

    2020-06-23 15:00

    阅读数 14

    python生成的时钟效果,大家可以参考:

    使用的python版本:3.7.0 ,以下为源代码:

    # time.py from turtle import * from datetime import * import time def

    SetupClock(radius): #建立表的外框 reset() pensize() for i in range(60): Skip(radius)

    if i % 5 == 0: forward(20) Skip(-radius-20) else: dot(5) Skip(-radius) right(6)

    def Skip(step): penup() forward(step) pendown() #定义表针函数mkHand() def

    mkHand(name, length): #注册Turtle形状,建立表针Turtle reset() Skip(-length*0.1)

    begin_poly() forward(length*1.1) end_poly() handForm = get_poly()

    register_shape(name, handForm) def Init(): global secHand, minHand, hurHand,

    printer mode("logo") # 重置Turtle指向北 #建立三个表针Turtle并初始化 mkHand("secHand", 125)

    mkHand("minHand", 130) mkHand("hurHand", 90) secHand = Turtle()

    secHand.shape("secHand") minHand = Turtle() minHand.shape("secHand") hurHand =

    Turtle() hurHand.shape("secHand") for hand in secHand, minHand, hurHand:

    hand.shapesize(1, 1, 3) hand.speed(0) #建立输出文字Turtle printer = Turtle()

    printer.hideturtle() printer.penup() def get_week_day(): week_day_dict = { 0 :

    '星期一', 1 : '星期二', 2 : '星期三', 3 : '星期四', 4 : '星期五', 5 : '星期六', 6 : '星期天', }

    today = int(time.strftime("%w")) return week_day_dict[today] #更新时钟函数Tick() def

    Tick(): #绘制表针的动态显示 t = datetime.today() second = t.second +

    t.microsecond*0.000001 minute = t.minute + second/60.0 hour = t.hour +

    minute/60.0 tracer(False) printer.forward(65) # print(get_week_day())

    printer.write(get_week_day(), align="center", font=("Courier", 14, "bold"))

    printer.back(130) printer.write((str(t.year)+"-"+str(t.month)+"-"+str(t.day)),

    align="center", font=("Courier", 14, "bold")) printer.home() tracer(True)

    secHand.setheading(6*second) minHand.setheading(6*minute)

    hurHand.setheading(30*hour) ontimer(Tick, 100) #100ms后继续调用tick def main():

    tracer(False) Init() SetupClock(160) tracer(True) Tick() mainloop() if __name__

    == '__main__': main()

    展开全文
  • python ——钟表

    2017-08-25 20:57:00
    1、表盘 1 from turtle import * 2 from datetime import * 3 4 def Skip(step):#表盘不连续地画图 5 penup() 6 forward(step) 7 pendown() 8 9 def SetupClock(radius): ...1...

    1、表盘

     1 from turtle import *
     2 from datetime import *
     3 
     4 def Skip(step):#表盘不连续地画图
     5     penup()
     6     forward(step)
     7     pendown()
     8 
     9 def SetupClock(radius):
    10     #建立表的外框
    11     reset()
    12     pensize(7)
    13     for i in range(60):
    14         Skip(radius)#跨越中间这段不画
    15         if i % 5 == 0:
    16             forward(20)
    17             Skip(-radius-20)#抬起画笔,回到原处
    18         else:
    19             dot(5)
    20             Skip(-radius)#抬起画笔,回到圆心
    21         right(6)#回到圆心,方向旋转6度
    22 
    23 SetupClock(160)

    2、添加指针

     1 from turtle import *
     2 from datetime import *
     3 
     4 def Skip(step):#表盘不连续地画图
     5     penup()
     6     forward(step)
     7     pendown()
     8 
     9 def mkHand(name, length):
    10     #注册Turtle形状,建立表针Turtle
    11     #清空窗口,重置默认值
    12     Skip(-length*0.1)#先回一点
    13     begin_poly()#初始点
    14     forward(length*1.1)
    15     end_poly()#结束点
    16     handForm = get_poly()#返回最后记录的多边形
    17     register_shape(name, handForm)#寄存器形状,安装相应的多边形
    18     goto(0,0)
    19 
    20 def Init():
    21     global secHand, minHand, hurHand, printer
    22     mode("logo")# 重置Turtle指向北
    23 
    24     #建立三个表针Turtle并初始化
    25     mkHand("secHand", 125)
    26     right(30)
    27     mkHand("minHand",  110)
    28     right(30)
    29     mkHand("hurHand", 90)
    30 
    31     secHand = Turtle()
    32     secHand.shape("secHand")
    33     minHand = Turtle()
    34     minHand.shape("minHand")
    35     hurHand = Turtle()
    36     hurHand.shape("hurHand")
    37     
    38     for hand in secHand, minHand, hurHand:
    39         hand.shapesize(1, 1, 3)
    40         hand.speed(0)
    41 
    42     #建立输出文字Turtle
    43     printer = Turtle()
    44     printer.hideturtle()
    45     printer.penup()
    46 
    47 #画表框    
    48 def SetupClock(radius):
    49     #建立表的外框
    50     reset()
    51     pensize(7)
    52     for i in range(60):
    53         Skip(radius)#跨越中间这段不画
    54         if i % 5 == 0:
    55             forward(20)
    56             Skip(-radius-20)#抬起画笔,回到原处
    57         else:
    58             dot(5)
    59             Skip(-radius)#抬起画笔,回到圆心
    60         right(6)#回到圆心,方向旋转6度
    61 
    62 Init()
    63 tracer(False)
    64 SetupClock(160)

     

    3、时间

     1 from datetime import *
     2 from turtle import *
     3 
     4 #时分秒
     5 t = datetime.today()
     6 print(t)
     7 print(t.microsecond)
     8 print(t.second)
     9 second = t.second + t.microsecond*0.000001
    10 minute = t.minute + second/60.0
    11 hour = t.hour + minute/60.0
    12 
    13 #星期
    14 print(t.weekday())
    15 week = ["星期一", "星期二", "星期三",
    16             "星期四", "星期五", "星期六", "星期日"]
    17 print(week[t.weekday()])
    18 
    19 #年月日
    20 y = t.year
    21 m = t.month
    22 d = t.day
    23 # return "%s %d %d" % (y, m, d)
    24 print(y,"-",m,"-",d)
    25 
    26 tracer(False)
    27 mode("logo")# 重置Turtle指向北
    28 printer = Turtle()
    29 printer.forward(65)
    30 printer.write(week[t.weekday()], align="center",font=("Courier", 14, "bold"))#日期标在图中
    31 printer.back(130)
    32 printer.write("%d-%d-%d"%(y,m,d),align="center",font=("Courier", 14, "bold"))#日期标在图中

     

     

     

     1 from datetime import *
     2 from turtle import *
     3 
     4 #时分秒
     5 t = datetime.today()
     6 print(t)
     7 print(t.microsecond)
     8 print(t.second)
     9 second = t.second + t.microsecond*0.000001
    10 minute = t.minute + second/60.0
    11 hour = t.hour + minute/60.0
    12 
    13 #星期
    14 print(t.weekday())
    15 week = ["星期一", "星期二", "星期三",
    16             "星期四", "星期五", "星期六", "星期日"]
    17 print(week[t.weekday()])
    18 
    19 #年月日
    20 y = t.year
    21 m = t.month
    22 d = t.day
    23 # return "%s %d %d" % (y, m, d)
    24 print(y,"-",m,"-",d)
    25 
    26 tracer(False)
    27 mode("logo")# 重置Turtle指向北
    28 printer = Turtle()
    29 printer.hideturtle()
    30 printer.penup()#提起画笔,便没了那两道轨迹
    31 printer.forward(65)
    32 printer.write(week[t.weekday()], align="center",font=("Courier", 14, "bold"))#日期标在图中
    33 printer.back(130)
    34 printer.write("%s-%d-%d"%(y,m,d),align="center",font=("Courier", 14, "bold"))#日期标在图中
    35 printer.home()
    36 tracer(True)

     1 from datetime import *
     2 from turtle import *
     3 
     4 def Skip(step):#表盘不连续地画图
     5     penup()
     6     forward(step)
     7     pendown()
     8 
     9 def mkHand(name, length):
    10     #注册Turtle形状,建立表针Turtle
    11     #清空窗口,重置默认值
    12     Skip(-length*0.1)#先回一点
    13     begin_poly()#初始点
    14     forward(length*1.1)
    15     end_poly()#结束点
    16     handForm = get_poly()#返回最后记录的多边形
    17     register_shape(name, handForm)#寄存器形状,安装相应的多边形
    18     goto(0,0)
    19 
    20 def Init():
    21     global secHand, minHand, hurHand, printer
    22     mode("logo")# 重置Turtle指向北
    23     right(90)
    24     #建立三个表针Turtle并初始化
    25     mkHand("secHand", 125)
    26     right(30)
    27     mkHand("minHand",  110)
    28     right(30)
    29     mkHand("hurHand", 90)
    30 
    31     secHand = Turtle()
    32     secHand.shape("secHand")
    33     minHand = Turtle()
    34     minHand.shape("minHand")
    35     hurHand = Turtle()
    36     hurHand.shape("hurHand")
    37     
    38     for hand in secHand, minHand, hurHand:
    39         hand.shapesize(1, 1, 3)
    40         hand.speed(0)
    41 
    42     #建立输出文字Turtle
    43     printer = Turtle()
    44     printer.hideturtle()
    45     printer.penup()
    46 
    47 #画表框    
    48 def SetupClock(radius):
    49     #建立表的外框
    50     reset()
    51     pensize(7)
    52     for i in range(60):
    53         Skip(radius)#跨越中间这段不画
    54         if i % 5 == 0:
    55             forward(20)
    56             Skip(-radius-20)#抬起画笔,回到原处
    57         else:
    58             dot(5)
    59             Skip(-radius)#抬起画笔,回到圆心
    60         right(6)#回到圆心,方向旋转6度
    61 
    62 tracer(False)
    63 Init()
    64 SetupClock(160)
    65 tracer(True)
    66 
    67 #时分秒
    68 t = datetime.today()
    69 print(t)
    70 print(t.microsecond)
    71 print(t.second)
    72 second = t.second + t.microsecond*0.000001
    73 minute = t.minute + second/60.0
    74 hour = t.hour + minute/60.0
    75 
    76 
    77 #星期
    78 print(t.weekday())
    79 week = ["星期一", "星期二", "星期三",
    80             "星期四", "星期五", "星期六", "星期日"]
    81 print(week[t.weekday()])
    82 
    83 #年月日
    84 y = t.year
    85 m = t.month
    86 d = t.day
    87 # return "%s %d %d" % (y, m, d)
    88 print(y,"-",m,"-",d)
    89 
    90 tracer(False)
    91 #mode("logo")# 重置Turtle指向北
    92 printer = Turtle()
    93 printer.penup()
    94 printer.hideturtle()
    95 printer.forward(65)
    96 printer.write(week[t.weekday()], align="center",font=("Courier", 14, "bold"))#日期标在图中
    97 printer.back(130)
    98 printer.write("%d-%d-%d"%(y,m,d), align="center",font=("Courier", 14, "bold"))#日期标在图中
    99 tracer(True)

     

     

    4、动态显示

    将时间和角度对应起来,换算到角度,然后和指针对应起来

     

     1 from turtle import *
     2 from datetime import *
     3  
     4 def Skip(step):#表盘不连续地画图
     5     penup()
     6     forward(step)
     7     pendown()
     8  
     9 def mkHand(name, length):
    10     #注册Turtle形状,建立表针Turtle
    11     reset()
    12     Skip(-length*0.1)
    13     begin_poly()#多边形第一个顶点
    14     forward(length*1.1)
    15     end_poly()
    16     handForm = get_poly()
    17     register_shape(name, handForm)
    18  
    19 def Init():
    20     global secHand, minHand, hurHand, printer
    21     mode("logo")# 重置Turtle指向北,为后续角度提供便利
    22     #建立三个表针Turtle并初始化
    23     mkHand("secHand", 125)
    24     mkHand("minHand",  115)
    25     mkHand("hurHand", 90)
    26     secHand = Turtle()
    27     secHand.shape("secHand")
    28     minHand = Turtle()
    29     minHand.shape("minHand")
    30     hurHand = Turtle()
    31     hurHand.shape("hurHand")
    32     for hand in secHand, minHand, hurHand:
    33         hand.shapesize(1, 1, 3)
    34         hand.speed(0)
    35     #建立输出文字Turtle
    36     printer = Turtle()
    37     printer.hideturtle()
    38     printer.penup()
    39 
    40 #画表框    
    41 def SetupClock(radius):
    42     #建立表的外框
    43     reset()
    44     pensize(7)
    45     for i in range(60):
    46         Skip(radius)#跨越中间这段不画
    47         if i % 5 == 0:
    48             forward(20)
    49             Skip(-radius-20)#抬起画笔,回到原处
    50         else:
    51             dot(5)
    52             Skip(-radius)#抬起画笔,回到圆心
    53         right(6)#回到圆心,方向旋转6度
    54          
    55 def Week(t):    
    56     week = ["星期一", "星期二", "星期三",
    57             "星期四", "星期五", "星期六", "星期日"]
    58     return week[t.weekday()]
    59  
    60 def Date(t):
    61     y = t.year
    62     m = t.month
    63     d = t.day
    64     return "%s %d %d" % (y, m, d)
    65  
    66 def Tick():
    67     #绘制表针的动态显示
    68     t = datetime.today()
    69     second = t.second + t.microsecond*0.000001
    70     minute = t.minute + second/60.0
    71     hour = t.hour + minute/60.0
    72     secHand.setheading(6*second)
    73     minHand.setheading(6*minute)
    74     hurHand.setheading(30*hour)
    75      
    76     tracer(False)  
    77     printer.forward(65)
    78     printer.write(Week(t), align="center",
    79                   font=("Courier", 14, "bold"))
    80     printer.back(130)
    81     printer.write(Date(t), align="center",
    82                   font=("Courier", 14, "bold"))
    83     printer.home()
    84 
    85     tracer(True)
    86  
    87     ontimer(Tick, 100)#100ms后继续调用tick
    88  
    89 def main():
    90     tracer(False)#不刷新,最后一次刷新,省时间
    91     Init()
    92     SetupClock(160)
    93     tracer(True)
    94     Tick()
    95     mainloop()
    96  
    97 if __name__ == "__main__":       
    98     main()

     

    转载于:https://www.cnblogs.com/ruo-li-suo-yi/p/7429647.html

    展开全文
  • Python编写时钟表turtle

    2019-04-29 09:21:31
    Python编写时钟表turtle改进了对于库的引用从import * 到import 改进了对于库的引用 当前的的Python时钟程序对于库的引用格式不太合理,我修改了库的导入方式,提高程序的可读性 从import * 到import 更加规范的引用...
  • 【实例简介】用Python做一个钟表【实例截图】【核心代码】from turtle import *from datetime import *def Skip(step):penup()forward(step)pendown()def mkHand(name, length):#注册Turtle形状,建立表针...
  • 主要为大家详细介绍了python使用Turtle库绘制动态钟表,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
  • 利用turtle模块绘制钟表60个点,并且标注1-12文字后,再利用时间模块,显示当前时间,让针走动起来。
  • python 相关 了解数学函数 基本的uo机相关
  • # coding=utf-8   import turtle from datetime import *   # 抬起画笔,向前运动一段距离放下 def Skip(step):  turtle.penup()  turtle.forward(step)  turtle.pendown() ...def mkHand(name, length): ...
  • 这就不得不说,python真的是一个非常神奇的编程语言,小编在浏览一些知识资料时候,根据内容,整合了解后,发现居然可以制作钟表,立刻来了兴趣,于是根据内容编写了以下代码,大家如果感兴趣的话,也可以来看下哦~...
  • 最近学习python,接触到turtle标准库,后面又看到利用python内置库turtle库画一个实时钟表。话不多说,直接上代码叭! import turtle from datetime import * def Skip(step): turtle.penup() turtle.forward(step...
  • 今天给大家分享1个非常实用的python技能——用Python做个能挂墙上的大钟表,先上成果视频:本项目用到的库主要有pygame、math、datetime等,另外还用到一些数学知识,勾股定理搞不明白的朋友就不要往下看了,可能会...
  • Python 绘图—使用Turtle库绘制动态钟表

    万次阅读 多人点赞 2017-02-26 21:05:37
    Python函数库众多,而且在不断更新,所以学习这些函数库最有效的方法,就是阅读Python官方文档。...绘制动态钟表的基本思路如下(面向对象的编程): 使用5个turtle对象 1个turtle:绘制外表盘 3个turt
  • from turtle import * # 引入绘图模块 from datetime import * # 引入时间模块 speed(10) # 设置笔速 def jump(x): # jump函数确定时间标距表心的距离 penup() # 抬笔,防止下面跳跃的时候有笔迹 ...
  • 文章来源:python的爬虫与数据分析之路作者:小dull鸟今天给大家分享1个非常实用的python技能——用Python做个能挂墙上的大钟表,先上成果视频:本项目用到的库主要有pygame、math、datetime等,另外还用到一些数学...
  • 天给大家分享1个非常实用的python技能——用Python做个能挂墙上的大钟表,先上成果视频: 本项目用到的库主要有pygame、math、datetime等,另外还用到一些数学知识,勾股定理搞不明白的朋友就不要往下看了,可能...
  • 100) turtle.right(90) turtle.showturtle() turtle.done() 效果图(反正就是很丑): 绘制钟表,它的核心就是利用箭头的角度变化来添加各个刻度,没有什么难度,但是确实比较麻烦,而且要注意钟表上刻度的长短设置...
  • 今天给大家分享1个非常实用的python技能——用Python做个能挂墙上的大钟表,先上成果视频: 本项目用到的库主要有pygame、math、datetime等,另外还用到一些数学知识,勾股定理搞不明白的朋友就不要往下看了,可能...
  • 下面的代码向大家展示了如何绘制一个钟表的表盘: import turtle #实例化一个屏幕对象 screen=turtle.Screen() #把屏幕的背景颜色设置为黄色 screen.bgcolor('yellow') #实例化一个小乌龟,命名为baby,这也就是...
  • Python基础 用Python实现时钟显示

    千次阅读 2018-04-10 13:08:45
    语言:Python IDE:Python.IDE编写时钟程序,要求根据时间动态更新 代码思路 需求:5个Turtle对象, 1个绘制外表盘+3个模拟表上针+1个输出文字 Step1:建立Turtle对象并初始化 Step2:静态表盘绘制 Step3:根据时钟...
  • Python概述0011.1计算机与计算机语言0021.1.1计算机的诞生与发展0021.1.2计算机语言概述0061.1.3翻译执行0081.2Python语言概述0091.2.1Python语言发展史0091.2.2Python语言的特点0111.2.3Python的应用领域0121.2.4...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 932
精华内容 372
关键字:

python钟表

python 订阅