最新公告
  • 欢迎您光临起源地模板网,本站秉承服务宗旨 履行“站长”责任,销售只是起点 服务永无止境!立即加入钻石VIP
  • 如何使用Python 实现秒表功能?

    正文概述    2020-06-03   272

    其实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实用知识,点击进入起源地模板网教学中心


    起源地下载网 » 如何使用Python 实现秒表功能?

    常见问题FAQ

    免费下载或者VIP会员专享资源能否直接商用?
    本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
    提示下载完但解压或打开不了?
    最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器下载的bug,建议用百度网盘软件或迅雷下载。若排除这种情况,可在对应资源底部留言,或 联络我们.。
    找不到素材资源介绍文章里的示例图片?
    对于PPT,KEY,Mockups,APP,网页模版等类型的素材,文章内用于介绍的图片通常并不包含在对应可供下载素材包内。这些相关商业图片需另外购买,且本站不负责(也没有办法)找到出处。 同样地一些字体文件也是这种情况,但部分素材会在素材包内有一份字体下载链接清单。
    模板不会安装或需要功能定制以及二次开发?
    请QQ联系我们

    发表评论

    还没有评论,快来抢沙发吧!

    如需帝国cms功能定制以及二次开发请联系我们

    联系作者

    请选择支付方式

    ×
    迅虎支付宝
    迅虎微信
    支付宝当面付
    余额支付
    ×
    微信扫码支付 0 元