时钟
代码:
# -*- coding: utf-8 -*- """ Created on Tue Jun 16 21:59:36 2020 @author: Administrator """ from turtle import * import threading import turtle import time import datetime import pygame import winsound # 导入此模块实现声音播放功能 def play_music(): filepath = r"刚刚好.mp3"; pygame.mixer.init() # 加载音乐 pygame.mixer.music.load('C:\\py\\刚刚好.mp3') pygame.mixer.music.play(start=0.0) #播放时长,没有此设置,音乐不会播放,会一次性加载完loops和start分别代表重复的次数和开始播放的位置 pygame.mixer.music.play(loops=0) pygame.mixer.music.fadeout(60000) # 设置音乐多久慢慢淡出结束 # 移动一段距离 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) #printer.home() tracer(True) def clock(): 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() myWin = turtle.Screen() turtle.setup(width=1400,height=750,startx=0,starty=0) turtle.bgpic(r'C:\pd\滑稽.gif') # 这样设置。 myWin.exitonclick() turtle.mainloop() def bgpic(self, picname=None): if picname is None: return self._bgpicname if picname not in self._bgpics: self._bgpics[picname] = self._image(picname) self._setbgpic(self._bgpic, self._bgpics[picname]) self._bgpicname = picname if __name__ == '__main__': threads = [] threads.append(threading.Thread(target=clock))#使用元组传递 threads.append(threading.Thread(target=play_music)) for t in threads: t.start() # 提示用户设置时间和分钟 my_hour = input("请输入时:") my_minute = input("请输入分:") flag = 1 while flag: t = time.localtime() # 当前时间的纪元值 fmt = "%H %M" now = time.strftime(fmt, t) # 将纪元值转化为包含时、分的字符串 now = now.split(' ') #以空格切割,将时、分放入名为now的列表中 hour = now[0] minute = now[1] i=0 if hour == my_hour and minute == my_minute: while i<=20: i=i+1 music = 'TekkenData/Sound/JAB_PUNISH.wav' winsound.PlaySound(music, winsound.SND_ALIAS) flag = 0


浙公网安备 33010602011771号