• 简易计时器开发


    一、项目场景:

    事情是这样的,学校给了一个网页,让我们去学习,网页超过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()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170

    2. 代码细分

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

    2.1 设置显示界面

    先造一个可显示的界面

    import tkinter
    root = tkinter.Tk()
    root.title('计时器')  # 界面标题
    root.geometry('500x200')
    root.resizable(True,True)  # 可以拉伸
    
    # 持续显示界面
    root.mainloop()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

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

    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()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52

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

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

    播放音乐函数

    def play():
        pygame.mixer.init()
        pygame.mixer.music.load(file)# file对应文件位置
        pygame.mixer.music.play()
        time.sleep(15)
        pygame.mixer.music.stop()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    倒计时函数

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    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'
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

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

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    停止计时函数

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    暂停计时函数

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    3. 打包文件

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

    四、效果展示

    演示视频点这里


  • 相关阅读:
    Ubuntu系统上传文件的多种方法-断网上传-安装包上传-物联网开发维护
    机器学习实战(一)Keras 人工神经网络简介
    如何使用SQL系列 之 如何在SQL中使用主键
    数组去重的六种方法
    Linux 驱动的一些笔记
    基于Openwrt系统架构,实现应用与驱动的实例。
    R3Live系列学习(五)R3Live源码阅读
    业务复习知识点Oracle查询
    咳嗽检测深度神经网络算法
    【MineCraft】-- 学习我的世界Mod制作引雷附魔书与事件
  • 原文地址:https://blog.csdn.net/qq_40940944/article/details/127857260