python之time模塊

Time

首先來一段簡單一點的,額,要不就先來看看time.sleep()?python

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秒後結束呢?windows

哈哈,很差意思,我說我不會寫,(我以爲這是一個值得讓人思考的問題,也就沒有寫上去了,由於有了想法纔有動力)app

那咱們來作一個簡單的鐘表吧ide

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

有不少的缺陷在裏面,可是也是個不錯的問題,值得讓人去深究oop

而後我想到用Tkinter去實現時鐘ui

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窗口界面,而後去實現一個簡單的倒計時功能spa

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

 

我那時候就出了不少問題,可是具體在哪出的,我也不得而知了
下面是包的問題.net

在python下ModuleNotFoundError: No module named 'Tkinter'問題的總結:

轉載:http://www.javashuo.com/article/p-xlmtcrtp-ga.htmlcode

具體是個什麼狀況,我想仍是得具體分析了,blog

相關文章
相關標籤/搜索