网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

使用Python实现秒表功能的方法

这篇文章给大家分享的是有关使用Python实现秒表功能的方法的内容。小编觉得挺实用的,因此分享给大家做个参考。一起跟随小编过来看看吧。

创新互联于2013年开始,先为黄岛等服务建站,黄岛等地企业,进行企业商务咨询服务。为黄岛企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

关于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实现秒表功能的方法
网页链接:http://bjjierui.cn/article/iphdge.html

其他资讯