如何使用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()
输出:

使用Tkinter创建秒表
现在让我们尝试使用Tkinter模块创建一个程序来创建秒表。
所需模块:我们将仅使用tkinter来创建gui,并且此程序中将不使用其他任何库。
# Python program to illustrate a stop watch
# using Tkinter


```java
#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学习网教学中心。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值