zoukankan      html  css  js  c++  java
  • python之time模块

    Time

    首先来一段简单一点的,额,要不就先来看看time.sleep()?

    import time
    count = 1
    tag = True
    name = 'script'
    pwd = '123'
    while tag:
        if count == 4:
            print('Too many time!')
            break
        username = input('Please enter your username:').strip()
        password = input('Please enter your password:')
        if not username:
            print('again!')
            continue
        elif not password:
            print('gaain')
            continue
        if username == name and password == pwd:
            print('Login successfully!')
    
            while tag:
                user_cmd = input('Please enter you order:'.strip())
                if user_cmd == 'q':
                    tag = False
                    break
                print('You type in thr command %s' % user_cmd)
        else:
            print('The user name or password you entered is incorrect,please re-enter.')
            print('You only have %s chance' % (3-count))
        count += 1
    print(['5秒后结束,再见!'])
    
    time.sleep(5)  # 也就是上面所述,让程序过了5秒后再结束

    那么问题来了,为什么后面要来一个5秒后结束,那为什么不整一个过了一秒打印一次,最后到了5秒后结束呢?

    哈哈,不好意思,我说我不会写,(我觉得这是一个值得让人思考的问题,也就没有写上去了,因为有了想法才有动力)

    那我们来做一个简单的钟表吧

    import threading,time
    
    global t
    def sayHello():
        print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))
    
        t = threading.Timer(1.0,sayHello)
        t.start()
    sayHello()
    Horologe

    有很多的缺陷在里面,但是也是个不错的问题,值得让人去深究

    然后我想到用Tkinter去实现时钟

    import Tkinter,sys,time
    root=Tkinter.Tk()
    root.minsize(500, 500)
    Label1=Tkinter.Label(text=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))
    Label1.pack()
    def trickit():
        currentTime=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
        Label1.config(text=currentTime)
        root.update()
        Label1.after(1000, trickit)
    Label1.after(1000, trickit)
    
    root.mainloop()
    View Code

    通过Tkinter制作windows窗口界面,然后去实现一个简单的倒计时功能

    from Tkinter import *
    import time
    import tkMessageBox
     
    class App:
        def __init__(self,master):
            frame = Frame(master)
            frame.pack()
            self.entryWidget = Entry(frame)
            self.entryWidget["width"] = 15
            self.entryWidget.pack(side=LEFT)
            self.hi_there = Button(frame, text="Start", command=self.start)
            self.hi_there.pack(side=LEFT)
            self.button = Button(frame, text="QUIT", fg="red", command=frame.quit)
            self.button.pack(side=LEFT)
             
        def start(self):
            text = self.entryWidget.get().strip()
            if text != "":
                num = int(text)
                self.countDown(num)
             
        def countDown(self,seconds):
            lbl1.config(bg='yellow')
            lbl1.config(height=3, font=('times', 20, 'bold'))
            for k in range(seconds, 0, -1):
                lbl1["text"] = k
                root.update()
                time.sleep(1)
            lbl1.config(bg='red')
            lbl1.config(fg='white')
            lbl1["text"] = "Time up!"
            tkMessageBox.showinfo("Time up!","Time up!")
     
        def GetSource():
            get_window = Tkinter.Toplevel(root)
            get_window.title('Source File?')
            Tkinter.Entry(get_window, width=30,
                          textvariable=source).pack()
            Tkinter.Button(get_window, text="Change",
                           command=lambda: update_specs()).pack()
      
    root = Tk()
    root.title("Countdown")
    lbl1 = Label()
    lbl1.pack(fill=BOTH, expand=1)
    app = App(root)
    root.mainloop()
    #该代码片段来自于: http://www.sharejs.com/codes/python/7826
    View Code

    我那时候就出了很多问题,但是具体在哪出的,我也不得而知了
    下面是包的问题

    在python下ModuleNotFoundError: No module named 'Tkinter'问题的总结:

    转载:https://blog.csdn.net/blueheart20/article/details/78763208

    具体是个什么情况,我想还是得具体分析了,

  • 相关阅读:
    基于TFTP协议的远程升级设计
    BZOJ 刷题记录 PART 6
    解决org.hibernate.LazyInitializationException: could not initialize proxy
    在不同版本号hdfs集群之间转移数据
    从零開始制作H5应用(4)——V4.0,增加文字并给文字加特效
    不再安全的 OSSpinLock
    @synchronized 再考察
    ReactiveCocoa
    怎样界定问题
    问题是什么
  • 原文地址:https://www.cnblogs.com/scriptchild/p/8966459.html
Copyright © 2011-2022 走看看