In the gym, I wrote her a Xiaomi timer in python to help others

Hi everyone, I’m Lex who likes to bully Superman.
Areas of expertise: python development, network security penetration, Windows domain control Exchange architecture
Today's focus: Python graphical interface development, to achieve a stopwatch timer function on Xiaomi mobile phones
If you have any questions or needs, please leave a message~~

Things are like this

Doing deadlifts with earphones in the gym on weekends

Suddenly walked over to a young lady in professional fitness attire

She smiled and asked if she could borrow her mobile phone and said her battery was dead

But I need time between sets. Isn't this delaying my exercise?

I dropped the barbell, got up and wrote a timer for her in python

Xiaomi's stopwatch function

The effect of the stopwatch timer on the Xiaomi mobile phone is as follows, which can be accurate in milliseconds.

Today’s python actual combat case, let’s take everyone

Use Python to implement a small program with similar functions.

Logic code

After the program starts, wait for input through the input() method

Use the Enter key to start the timer

After turning it on, the stopwatch starts to beat, stop timing by pressing Ctrl+C

And output the total time.

The effect is as follows:

code show as below

import timeprint('按下回车开始计时,按下 Ctrl + C 停止计时。')while True:    input("")    starttime = time.time()    print(starttime)    print('开始计时')    try:        while True:            print('计时: ', round(time.time() - starttime, 2), '秒', end="\r")            time.sleep(0.001)    except KeyboardInterrupt:        print('结束')        endtime = time.time()        print('总用时为:', round(endtime - starttime, 2),'secs')        break

Python graphical interface

After the logical realization, we design an interface to display the time through the python GUI design module tkinter.

python interface design

1. Set the interface title and background color

import tkinterroot = tkinter.Tk()root.title("秒表")root['background']='black'root.mainloop()

The effect is as follows:

2. Set the window size

Set window size + label display time

import tkinterroot = tkinter.Tk()root.title("秒表")root['background']='black'#设置窗口大小root.geometry("300x500+200+50")#显示labeltime='00:05.35'label=tkinter.Label(root,text=time,font=("Arial",26),bg="black",fg="white")label.pack()#界面显示root.mainloop()

3. The parameter is passed into the label label

Pass the stopwatch timing time into the label label of the interface for display

Thereby forming a stopwatch display effect.

End of story

After writing it to her, she froze for a moment, then gave me another look

And then walked away a little angrily

And I can finally continue training without interruption

Python actual combat

[Python actual combat] ex-girlfriend’s wedding, python cracked the WIFI at the wedding site and changed the name to

[Python actual combat] The encrypted "520 Happy.pdf" sent by my ex-girlfriend, after I cracked it with python, I found

[Python actual combat] Last night, I used python to take a selfie with my sister P next door, and then found ...

[Python actual combat] Girlfriend works overtime in the middle of the night and sends a selfie. Python boyfriend discovers the amazing secret with 30 lines of code

[Python actual combat] python you TM is too skinny-only 30 lines of code can record every move of the keyboard

[ Python actual combat] Forgot the password of the goddess album, I only wrote 20 lines of code in Python~~~

pygame series articles

Let's learn pygame together. 30 cases of game development (2)-tower defense game

Let's learn pygame together. 30 cases of game development (4)-Tetris game

Let's learn pygame together. 30 cases of game development (5)-Xiaoxiaole game

Penetration testing practical column

Windows AD/Exchange Management Column

Linux high-performance server construction

PowerShell Automation Column

CSDN official learning recommendation ↓ ↓ ↓

The Python full-stack knowledge graph produced by CSDN is too strong, and I recommend it to everyone!