简易计时器开发

一、项目场景:

事情是这样的,学校给了一个网页,让我们去学习,网页超过5分钟无操作会自动跳出,需要一个定时器来提醒我们每隔一段时间去操作网页,我在网上查了几个定时器,都不太符合要求,于是自己动手做了一个4min的计时器。


二、 问题描述

1.设计计时器界面(包括按键与倒计时页面)
2.设置定时器
3.打包文件

三 问题实现:

使用语言当然是python了,这个我熟悉。

1. 编写代码

# 导入包
import time
import tkinter
import tkinter.filedialog
import threading
import pygame   # pip

# 窗口初试值
root = tkinter.Tk()
root.title('计时器')  # 界面标题
root.geometry('500x200')
root.resizable(True,True)  # 可以拉伸

# 文件初始值
file =''
global clock
global pause_mark
pause_mark = True

# 1窗口选择
def closeWindow():
    """
    关闭窗口
    :return:
    """
    # 修改变量,结束线程中的循环

    global playing

    playing = False

    time.sleep(0.3)

    try:

        # 停止播放,如果已停止,

        # 再次停止时会抛出异常,所以放在异常处理结构中

        pygame.mixer.music.stop()

        pygame.mixer.quit()

    except:

        pass

    root.destroy()
# 2选择音频文件
def buttonChooseClick():

    #添加文件夹
    global file
    global res
    if not file:
        file = tkinter.filedialog.askopenfilename(filetypes=[("MP3",".mp3"),("WAV",".wav"),("OGG",".ogg")])

    # 根据情况禁用和启用相应的按钮
    buttonPlay['state'] = 'normal'
    buttonChoose['state'] = 'disable'
# 3.开始计时
def Startclock():
    global clocking, i
    clocking = True
    i = 0
    t = threading.Thread(target=count)
    t.start()
    # 根据情况禁用和启用相应的按钮
    buttonStop['state'] = 'normal'
    buttonPause['state'] = 'normal'
    #buttonPlay['state'] = 'disabled'
    pass
# 4.停止计时
def Stopclock():
    # 根据情况禁用和启用相应的按钮
    timeName.set("重新开始计时吧")
    global clocking,i
    clocking = False
    i =250
    t = threading.Thread(target=count)
    t.start()
    t1 = threading.Thread(target=play)
    t1.start()
    pass
# 5. 暂停计时
def Pauseclock():
    global pause_mark
    global clocking
    if (pause_mark == True):
        clocking = False
        t = threading.Thread(target=count)
        t.start()
        pause_mark = False
    else:
        clocking = True
        t = threading.Thread(target=count)
        t.start()
        pause_mark = True

    t1 = threading.Thread(target=play)
    t1.start()

    pass
def count(): #计时器
    global clocking,i
    while clocking: # 设置停止标志
        if (i <240):
            time.sleep(1)
            second= 240-i
            show_min =int (second/60)
            show_second =second%60
            out_char = "0"+str(show_min)+" :"+str(show_second)
            timeName.set(out_char)
            i = i+1
        if (i == 240):
            t1 = threading.Thread(target=play)
            t1.start()
            clocking =False
            t1 = threading.Thread(target=count)
            t1.start()
        if(i==250):
            timeName.set("重新开始计时吧")
        else:
            pass

def play():
    pygame.mixer.init()
    pygame.mixer.music.load(file)
    pygame.mixer.music.play()
    time.sleep(15)
    pygame.mixer.music.stop()



# 窗口关闭
#root.protocol('WM_DELETE_WINDOW', closeWindow)

# 选择音乐
buttonChoose = tkinter.Button(root,text='选择音乐',command=buttonChooseClick)# 添加按钮
buttonChoose.place(x=50,y=10,width=100,height=20)# 布局

# 开始计时按钮
pause_resume = tkinter.StringVar(root,value='开始计时')
buttonPlay = tkinter.Button(root,textvariable=pause_resume,command=Startclock)
buttonPlay.place(x=150,y=10,width=100,height=20)# 布局,按键位置以及大小
buttonPlay['state'] = 'disabled' #  初始化废掉你

# 停止计时按钮
buttonStop = tkinter.Button(root,text='停止计时',command=Stopclock)
buttonStop.place(x=250,y=10,width=100,height=20)# 布局,按键位置以及大小
buttonStop['state'] = 'disabled' #  初始化废掉你

# 暂定计时按钮
buttonPause = tkinter.Button(root,text='暂停计时',command=Pauseclock)
buttonPause.place(x=350,y=10,width=100,height=20)# 布局,按键位置以及大小
buttonPause['state'] = 'disabled' #  初始化废掉你


# 倒计时标签
labelName = tkinter.Label(root, text="倒计时")
labelName.place(x=50, y=50, width=50, height=20)


# 倒计时数值标签
timeName = tkinter.StringVar(root, value=0)
labeltime = tkinter.Label(root, textvariable=timeName)
labeltime.place(x=150, y=50, width=260, height=20)

# 持续显示界面
root.mainloop()

2. 代码细分

整个代码是不是挺复杂的,所以这部分我们将整个代码拆分,便于大家逐步理解。

2.1 设置显示界面

先造一个可显示的界面

import tkinter
root = tkinter.Tk()
root.title('计时器')  # 界面标题
root.geometry('500x200')
root.resizable(True,True)  # 可以拉伸

# 持续显示界面
root.mainloop()

输出是这个界面了
在这里插入图片描述

2.2 添加按键

在原有界面的基础上添加按键

import tkinter

#1. 这是每个按键绑定的函数
#由于这儿主要介绍按键,所以这里的函数都简化了
def buttonChooseClick():
    pass
def Startclock():
    pass
def Stopclock():
    pass
def Pauseclock():
    pass
#2.界面
root = tkinter.Tk()
root.title('计时器')  # 界面标题
root.geometry('500x200')
root.resizable(True,True)  # 可以拉伸

#3. 这是所有的按键
## 选择音乐按钮
buttonChoose = tkinter.Button(root,text='选择音乐',command=buttonChooseClick)# 添加按钮
buttonChoose.place(x=50,y=10,width=100,height=20)# 布局

## 开始计时按钮
pause_resume = tkinter.StringVar(root,value='开始计时')
buttonPlay = tkinter.Button(root,textvariable=pause_resume,command=Startclock)
buttonPlay.place(x=150,y=10,width=100,height=20)# 布局,按键位置以及大小
buttonPlay['state'] = 'disabled' #  初始化废掉你

## 停止计时按钮
buttonStop = tkinter.Button(root,text='停止计时',command=Stopclock)
buttonStop.place(x=250,y=10,width=100,height=20)# 布局,按键位置以及大小
buttonStop['state'] = 'disabled' #  初始化废掉你

## 暂定计时按钮
buttonPause = tkinter.Button(root,text='暂停计时',command=Pauseclock)
buttonPause.place(x=350,y=10,width=100,height=20)# 布局,按键位置以及大小
buttonPause['state'] = 'disabled' #  初始化废掉你


## 倒计时标签
labelName = tkinter.Label(root, text="倒计时")
labelName.place(x=50, y=50, width=50, height=20)


## 倒计时数值标签
timeName = tkinter.StringVar(root, value=0)
labeltime = tkinter.Label(root, textvariable=timeName)
labeltime.place(x=150, y=50, width=260, height=20)

# 持续显示界面
root.mainloop()

一起看看效果
在这里插入图片描述

2.3 倒计时函数与播放音乐函数

播放音乐函数

def play():
    pygame.mixer.init()
    pygame.mixer.music.load(file)# file对应文件位置
    pygame.mixer.music.play()
    time.sleep(15)
    pygame.mixer.music.stop()

倒计时函数

def count(): #计时器
    global clocking,i
    while clocking: # 设置停止标志
        if (i <240):
            time.sleep(1)
            second= 240-i
            show_min =int (second/60)
            show_second =second%60
            out_char = "0"+str(show_min)+" :"+str(show_second)
            timeName.set(out_char)
            i = i+1
        if (i == 240):
            t1 = threading.Thread(target=play)
            t1.start()
            clocking =False
            t1 = threading.Thread(target=count)
            t1.start()
        if(i==250):
            timeName.set("重新开始计时吧")
        else:
            pass

2.4 按键所绑定的函数

选择音乐函数

选择音乐函数,该音乐用于计时结束后的提醒,按"停止计时"按键的提醒,按“”暂停计时“”按键的提醒。

def buttonChooseClick():

    #添加文件夹
    global file
    global res
    if not file:
        file = tkinter.filedialog.askopenfilename(filetypes=[("MP3",".mp3"),("WAV",".wav"),("OGG",".ogg")])

    # 根据情况禁用和启用相应的按钮
    buttonPlay['state'] = 'normal'
    buttonChoose['state'] = 'disable'

按“选择音乐”按键后输出是这样的。
在这里插入图片描述
开始计时函数

def Startclock():
    global clocking, i
    clocking = True
    i = 0
    t = threading.Thread(target=count)
    t.start()
    # 根据情况禁用和启用相应的按钮
    buttonStop['state'] = 'normal'
    buttonPause['state'] = 'normal'
    #buttonPlay['state'] = 'disabled'
    pass

停止计时函数

def Stopclock():
    # 根据情况禁用和启用相应的按钮
    timeName.set("重新开始计时吧")
    global clocking,i
    clocking = False
    i =250
    t = threading.Thread(target=count)
    t.start()
    t1 = threading.Thread(target=play)
    t1.start()
    pass

暂停计时函数

def Pauseclock():
    global pause_mark
    global clocking
    if (pause_mark == True):
        clocking = False
        t = threading.Thread(target=count)
        t.start()
        pause_mark = False
    else:
        clocking = True
        t = threading.Thread(target=count)
        t.start()
        pause_mark = True

    t1 = threading.Thread(target=play)
    t1.start()

    pass

3. 打包文件

打包.py文件为.exe文件
我参考这个博主的教程
最详细Python打包exe教程,并修改图标,只需30秒
操作比价容易,一起看看打包后的文件
在这里插入图片描述

四、效果展示

演示视频点这里


本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>