python之time模組

來源:互聯網
上載者:User

標籤:csdn   date()   loop   imp   word   roo   password   lap   ram   

Time

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

import timecount = 1tag = Truename = ‘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 += 1print([‘5秒後結束,再見!‘])time.sleep(5)  # 也就是上面所述,讓程式過了5秒後再結束

那麼問題來了,為什麼後面要來一個5秒後結束,那為什麼不整一個過了一秒列印一次,最後到了5秒後結束呢?

哈哈,不好意思,我說我不會寫,(我覺得這是一個值得讓人思考的問題,也就沒有寫上去了,因為有了想法才有動力)

那我們來做一個簡單的鐘錶吧

import threading,timeglobal tdef 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,timeroot=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 timeimport 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‘問題的總結:

轉載:78763208

具體是個什麼情況,我想還是得具體分析了,

 

python之time模組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.