一个玩得好的朋友突然大晚上的给我布置作业,吓得我精神抖擞。他的原话是:
感觉发现新大陆了,利用小海龟画的,上次玩海龟已经是几个月之前的事情了
https://blog.csdn.net/hanhanwanghaha/article/details/106211876
话不多说,上代码!
https://blog.csdn.net/hanhanwanghaha宝藏女孩 欢迎您的关注!
欢迎关注微信公众号:宝藏女孩的成长日记
如有转载,请注明出处(如不注明,盗者必究)
动态时钟
from turtle import *
from datetime import *# 移动到指定位置
def skip(step):penup()forward(step)pendown()# 画指针
def drawpointer(name, length):reset()skip(-length * 0.1)begin_poly()forward(length * 1.1)end_poly()handForm = get_poly()register_shape(name, handForm)# 初始化
def init():global hrpointer, minpointer, secpointer, weektext# 设置turtle Logo模式,朝北mode("logo")drawpointer("hrpointer", 90)drawpointer("minpointer", 130)drawpointer("secpointer", 140)hrpointer = Turtle()hrpointer.shape("hrpointer")minpointer = Turtle()minpointer.shape("minpointer")secpointer = Turtle()secpointer.shape("secpointer")secpointer.pencolor("red")for pointer in hrpointer, minpointer, secpointer:pointer.shapesize(3, 1, 1)pointer.speed(0)# 文字输出weektext = Turtle()weektext.hideturtle()weektext.penup()# 设置表盘形状
def setupClock(radius):#绘制表的外框reset()# clockPanel = Turtle()pensize(7)pencolor("blue")for i in range(60):skip(radius)if i % 5 == 0:forward(20)#画长刻度线skip(-radius - 20)#回到中心点else:dot(5)skip(-radius)right(6)# 星期文本
def Week(t):week = ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"]return week[t.weekday()]# 日期文本
def Date(t):y = t.yearm = t.monthd = t.dayreturn "%s %d %d" % (y, m, d)# 绘制表针动态显示
def tick():t = datetime.today()second = t.second + t.microsecond * 0.0000001minute = t.minute + second / 60.0hour = t.hour + minute / 60.0secpointer.setheading(6 * second)minpointer.setheading(6 * minute)hrpointer.setheading(30 * hour)tracer(False)weektext.forward(65)weektext.write(Week(t), align="center", font=("Courier", 14, "bold"))weektext.back(130)weektext.write(Date(t), align="center", font=("Courier", 14, "bold"))weektext.home()tracer(True)# 间隔100ms调用一次ontimer(tick, 100)def main():#打开/关闭海龟画,并为更新图纸设置延迟tracer(False)init()setupClock(160)tracer(True)tick()mainloop()if __name__ == '__main__':main()
代码借鉴(部分添加修改):https://www.jb51.net/article/151049.htm
算出某月星期几的所有日期
from datetime import date, timedelta, datetime
import calendar# 输入想要获取的年月份
cal = calendar.month(2020, 9)
print(cal)m = datetime.now().month
y = datetime.now().year
ndays = (date(y, m + 1, 1) - date(y, m, 1)).days
day_one = date(y, m, 1)
last_day = date(y, m, ndays)
delta = last_day - day_one
data_list = []
for i in range(delta.days + 1):p = (day_one + timedelta(days=i)).strftime('%Y-%m-%d')pp = datetime.strptime(str(p), '%Y-%m-%d')one = pp.isoweekday()print(type(one), one)if one == 1: #此处的1为星期一d2 = pp.strftime('%Y-%m-%d')data_list.append(d2)print("2020年9月份所有的星期一的日期为", data_list)#实时获取当前时间 转换为str字符串
now = datetime.now()
print('当前时间为 :', now)
运行结果
https://blog.csdn.net/hanhanwanghaha宝藏女孩 欢迎您的关注!
欢迎关注微信公众号:宝藏女孩的成长日记
如有转载,请注明出处(如不注明,盗者必究)