知行编程网知行编程网  2022-11-16 10:30 知行编程网 隐藏边栏  6 
文章评分 0 次,平均分 0.0
导语: 本文主要介绍了关于如何使用Python 实现秒表功能?的相关知识,希望可以帮到处于编程学习途中的小伙伴


其实


Python 并不像我们看到的那么复杂。如果我们打好基础,我们可以用python做一些有趣的事情,比如实现秒表功能。一起来看看吧~




前言:




本文的重点是在

python中使用Tkinter创建秒表。




关于




Tkinter:


Tkinter 是 Python 的标准 GUI 库。 Python 与 Tkinter 结合使用时,提供了一种快速简便的方式来创建 GUI 应用程序。 Tkinter 为 Tk GUI 工具包提供了一个强大的面向对象接口。开始使用 Tkinter 很容易,这里有一些示例代码可以帮助你在 python 中开始使用 Tkinter。




语法:


# Python program to create a
# a new window using Tkinter
# importing the required libraires
import tkinter
  
# creating a object 'top' as instance of class Tk
top = tkinter.Tk()
  
# This will start the blank window
top.mainloop()




输出:


如何使用 Python 实现秒表功能?



使用Tkinter创建秒表



现在让我们尝试使用

Tkinter模块创建一个程序来创建秒表。




所需模块:




我们将仅使用

tkinter来创建gui,并且此程序中将不使用其他任何库。

# Python program to illustrate a stop watch
# using Tkinter
#importing the required libraries
import tkinter as Tkinter
  
counter = -1
running = False
def counter_label(label):
    def count():
        if running:
            global counter
  
            # To manage the intial delay.
            if counter==-1:             
                display="Starting..."
            else:
                display=str(counter)
  
            label['text']=display   # Or label.config(text=display)
  
            # label.after(arg1, arg2) delays by  
            # first argument given in milliseconds
            # and then calls the function given as second argument.
            # Generally like here we need to call the  
            # function in which it is present repeatedly.
            # Delays by 1000ms=1 seconds and call count again.
            label.after(1000, count)  
            counter += 1
  
    # Triggering the start of the counter.
    count()      
  
# start function of the stopwatch
def Start(label):
    global running
    running=True
    counter_label(label)
    start['state']='disabled'
    stop['state']='normal'
    reset['state']='normal'
  
# Stop function of the stopwatch
def Stop():
    global running
    start['state']='normal'
    stop['state']='disabled'
    reset['state']='normal'
    running = False
  
# Reset function of the stopwatch
def Reset(label):
    global counter
    counter=-1
  
    # If rest is pressed after pressing stop.
    if running==False:       
        reset['state']='disabled'
        label['text']='Welcome!'
  
    # If reset is pressed while the stopwatch is running.
    else:                
        label['text']='Starting...'
  
root = Tkinter.Tk()
root.title("Stopwatch")
  
# Fixing the window size.
root.minsize(width=250, height=70)
label = Tkinter.Label(root, text="Welcome!", fg="black", font="Verdana 30 bold")
label.pack()
start = Tkinter.Button(root, text='Start',  
width=15, command=lambda:Start(label))
stop = Tkinter.Button(root, text='Stop',  
width=15, state='disabled', command=Stop)
reset = Tkinter.Button(root, text='Reset',
 width=15, state='disabled', command=lambda:Reset(label))
start.pack()
stop.pack()
reset.pack()
root.mainloop()




输出:


如何使用 Python 实现秒表功能?

如何使用 Python 实现秒表功能?


好了,以上就是使用


Python 实现秒表功能的全部内容了,如需了解更多python实用知识,点击进入

本文为原创文章,版权归所有,欢迎分享本文,转载请保留出处!

知行编程网
知行编程网 关注:1    粉丝:1
这个人很懒,什么都没写
扫一扫二维码分享