Simple timer development

1. Project scenario:

The thing is like this, the school gave us a webpage, let us go to study, the webpage will automatically jump out if there is no operation for more than 5 minutes, we need a timer to remind us to operate the webpage every once in a while, I checked several timers on the Internet , did not meet the requirements, so I made a 4min timer by myself.


2. Problem description

1. Design timer interface (including button and countdown page)
2. Set timer
3. Pack files

Three problems are realized:

The language used is of course python, which I am familiar with.

1. Write code

# 导入包
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. Code subdivision

Is the whole code quite complicated, so we will split the whole code in this part, so that everyone can understand it step by step.

2.1 Setting the display interface

First create a displayable interface

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

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

The output is this interface
insert image description here

2.2 Add button

Add buttons on the basis of the original interface

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()

Let's see the effect together
insert image description here

2.3 Countdown function and play music function

play music function

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

countdown function

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 Functions bound to keys

select music function

Select the music function, the music is used for the reminder after the timing is over, the reminder of pressing the "stop timing" button, and the reminder of pressing the ""pause timing"" button.

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'

This is the output after pressing the "Select Music" button.
insert image description here
start timer function

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

stop timer function

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

Pause timer function

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. Package files

Package the .py file into an .exe file.
I refer to this blogger’s tutorial
for the most detailed Python packaging exe tutorial, and modify the icon. It only takes 30 seconds. The
operation is easy to compare and the price is easy. Let’s take a look at the packaged file
insert image description here

4. Effect display

Demo video click here


Guess you like

Origin blog.csdn.net/qq_40940944/article/details/127857260