【重要通知】起床困难户:这个闹钟 App,彻底治好了我的拖延症、收藏收藏
  AGvOXTF2xgLB 2023年11月02日 32 0


导语 

 相信有不少人的闹钟是设成这样的:

6:20

6:30

6:35

6:37

……

起床真是令人困扰的事情,有的人根本不用定闹钟,但有的人提前半个小时闹钟都叫不醒,你的闹

钟怎么定的?

【重要通知】起床困难户:这个闹钟 App,彻底治好了我的拖延症、收藏收藏_闹钟

举个粒子:

现在这天气真的就很需要闹钟,每天8点不然真的醒不来

两个闹钟都喊不起,最少需要三个闹钟。需要,而且每5分钟一个。

我是起床困难户{需要5-6个闹钟},但我能克服熬夜早点睡的时候,第二天只需要二、三个闹钟就能起来了。

我的闹钟都是每天晚上亲自检查好然后设置的,因为担心漏掉了,怕早上迟到。

需要三个闹钟,2个隔比较近,1个隔10分钟响,让自己觉得赖床已经好久,更开心的起床......

哈哈哈——今天的话就教教大家来制作一款小闹钟小程序~卡卡通通的外表才适合我嘛!

【重要通知】起床困难户:这个闹钟 App,彻底治好了我的拖延症、收藏收藏_python_02

正文

这款可爱的小闹钟是基于Tkinter做的界面哈——有界面更好看滴!

做的界面也是卡通的动漫的形象啦,当然也是可以随意修改很多界面背景、参数的哈。

一、小科普

1)time 模块

 在Python中包含了若干个能够处理时间的库,而time库是最基本的一个,是Python中处理时间的

标准库。time库能够表达计算机时间,提供获取系统时间并格式化输出的方法,提供系统级精确计

时功能(可以用于程序性能分析)。


  time库包含三类函数,以下介绍常用的函数:

  • 时间获取:time()、ctime()、gmtime()
  • 时间格式化:strftime()、strptime()
  • 程序计时:sleep()、perf_counter()


2)messagebox模块

messagebox可用在消息提示框、警告框、询问框、错误、关于等会话框,messagebox可以很方便

实现相关对话框的弹出。

二、准备中

1)环境安装

该项目运行环境:Python3、Pycharm、Pygame、Tkinter、time模块等一些自带的不一一介绍了哈。库的安装统一使用:

pip install +模块名 或带豆瓣镜像源 pip install -i https://pypi.douban.com/simple/ +模块名

2)素材(音乐+背景可修改)

【重要通知】起床困难户:这个闹钟 App,彻底治好了我的拖延症、收藏收藏_python_03

三、开始敲代码

1)导入模块

from os import environ
environ['PYGAME_HIDE_SUPPORT_PROMPT'] = '1'
import pygame
import time # 导入此模块,获取当前时间
from tkinter import *
from tkinter import messagebox #弹窗
import threading

2)界面设置

标题,界面的大小宽度,文字等等都设置好了哈。

def sleeptime(hour, min, sec):
return hour * 3600 + min * 60 + sec
def GUI_TIME():
window = Tk()
window.title('来自一位小可爱')
window.geometry('505x430+300+100')
canvas = Canvas(window, height=500, width=500)
image_file = PhotoImage(file='01.gif')
image = canvas.create_image(0, 0, anchor='nw', image=image_file)
canvas.pack(side='top')
Label(window, text='添加闹钟: ').place(x=130, y=200)
Label(window,text = "帅的人已经醒了,丑的人还在睡觉",fg = 'red').place(x = 140,y = 80)
var_usr_name = StringVar()
var_usr_name.set('24:59')
entry_usr_name = Entry(window, textvariable=var_usr_name, background='pink')
entry_usr_name.place(x=250, y=200)

3)获取当前实时时间

def gg1():
index = 0
try:
my_hour,my_minute = start()
except:
messagebox.showwarning("提醒","请注意:是否为中文输入法的:")
index = 1
if(index == 0):
window_sign_up = Toplevel(window)
window_sign_up.attributes("-toolwindow", 1)
window_sign_up.wm_attributes("-topmost", 1)
window_sign_up.geometry('200x100+400+150')
Label(window_sign_up, text='当前时间为: ').place(x=0, y=10)
window_sign_up.title('顾木子吖')
b1 = Button(window_sign_up, text=' 退出 ', command=window.quit,fg = 'red')
b1.place(x=50, y=50)
str = StringVar()
l = Label(window_sign_up, textvariable=str,fg = 'red').place(x=80, y=10)
cw = 1
while cw == 1:
t = time.localtime() # 当前时间的纪元值
fmt = "%H %M"
times = "%H : %M : %S"
now = time.strftime(fmt, t) # 将纪元值转化为包含时、分的字符串
times = time.strftime(times, t) #显示的时间
now = now.split(' ')
hour = now[0]
minute = now[1]
str.set(times)
if (hour == my_hour and minute == my_minute):
str.set("时间到了")
play_music()
time.sleep(0.95)

def start():
timea = var_usr_name.get()
my_hourtotal = timea.split(":") # 时间
my_hour = my_hourtotal[0] # 小时
my_minute = my_hourtotal[1] # 分钟
return my_hour,my_minute
def ff1():
try:
ff()
except:
messagebox.showwarning("请注意:是否为中文输入法的:")
def ff():
thread = threading.Thread(target=gg1)
thread.daemon = True
thread.start()

4)设置开始、退出按钮

btn_sign_up = Button(window, text=' 开始 ', command=lambda: ff())
btn_sign_up.place(x=130, y=300)
b1 = Button(window, text=' 退出 ', command=window.quit)

b1.place(x=250, y=300)
b2 = Button(window,text = " ? ",command = problem_mail)
b2.place(x=420,y=390)
window.mainloop()

5)设置到点播放音乐

def play_music():
#str.set("时间到了")
filepath = r"良人乐团 - 春节的鞭炮.mp3";
pygame.mixer.init()
# 加载音乐
pygame.mixer.music.load(filepath)
pygame.mixer.music.play(start=0.0)
# 播放时长,没有此设置,音乐不会播放,会一次性加载完
time.sleep(290)
pygame.mixer.music.stop()

6)额外设置的一个弹窗提醒

def problem_mail():
messagebox.showinfo("看文末")

7)效果展示

7.1 随机展示part1

“帅的人已经起来了,丑的人还在睡觉”

【重要通知】起床困难户:这个闹钟 App,彻底治好了我的拖延症、收藏收藏_pygame_04


7.2 随机展示part2

“就算全世界把你遗忘,闹钟都会记得你”

【重要通知】起床困难户:这个闹钟 App,彻底治好了我的拖延症、收藏收藏_闹钟_05

总结

这不?这个闹钟界面又可爱颜值又高,没有人会拒绝的叭,以后要抢购啥的还可以多设置几个闹钟

准时起来抢东西了哈。

下面这款闹钟是真的一秒醒哈哈哈!

【重要通知】起床困难户:这个闹钟 App,彻底治好了我的拖延症、收藏收藏_python_06

完整的素材、安装环境、源码等看文末即可啦!

【重要通知】起床困难户:这个闹钟 App,彻底治好了我的拖延症、收藏收藏_闹钟_07


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

AGvOXTF2xgLB