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.
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
pygame series articles
CSDN official learning recommendation ↓ ↓ ↓
The Python full-stack knowledge graph produced by CSDN is too strong, and I recommend it to everyone!