python's time module

Time

Let's start with a simpler one, um, why don't we take a look at time.sleep() first?

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 ([ ' end in 5 seconds, bye! ' ])

time.sleep( 5)   #That is, as mentioned above, let the program end after 5 seconds

So the question is, why does it end after 5 seconds, then why not print the whole one after one second, and finally end after 5 seconds?

Haha, I'm sorry, I said I can't write, (I think this is a question worth thinking about, so I didn't write it up, because I have ideas to be motivated)

So let's make a simple clock

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

There are a lot of flaws in it, but it's also a good problem, and it's worth looking into

Then I thought of using Tkinter to implement the clock

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

Make a windows window interface through Tkinter, and then implement a simple countdown function

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 ()
#The code snippet is from: http://www.sharejs.com/codes/python/7826
View Code

 

I had a lot of problems at that time, but where did it come from, I don't know what
the problem is with the package.

Summary of the problem of ModuleNotFoundError: No module named 'Tkinter' under python:

Reprinted: https://blog.csdn.net/blueheart20/article/details/78763208

What is the specific situation, I think it still has to be analyzed in detail.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325009480&siteId=291194637