1. 程式人生 > >python 使用者登入介面及code

python 使用者登入介面及code

1.輸入使用者名稱密碼
2.認證成功後顯示歡迎資訊
3.輸錯三次後鎖定
# -*- coding: utf-8 -*-
# -*- coding: cp936 -*-

import getpass
import Tkinter as tk
import time
import datetime
import tkMessageBox


"""
1.輸入使用者名稱密碼
2.認證成功後顯示歡迎資訊
3.輸錯三次後鎖定

"""

count = 0

def tick():
    global time1
    # 從執行程式的計算機上面獲取當前的系統時間
    time2 = time.strftime('%Y-%m-%d %H:%M:%S')
    # 如果時間發生變化,程式碼自動更新顯示的系統時間
    if time2 != time1:
        time1 = time2
        clock.config(text=time2)
        # calls itself every 200 milliseconds
        # to update the time display as needed
        # could use >200 ms, but display gets jerky
    clock.after(200, tick)
    
def handler():
    
    '''事件處理函式'''
    global count
    global t1, t2
    username = entry.get()
    password = entry2.get()
  
    if count > 3:
        t2 = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
        t2= datetime.datetime.strptime(t2,'%Y-%m-%d %H:%M:%S')
        dateline = datetime.timedelta( seconds=12,  minutes=0, hours=0)
        if (dateline - (t2 - t1)).days == 0:
            print (u'密碼輸入錯誤超過3次,該賬號已被鎖定,請'),
            print dateline - (t2 - t1),
            print (u'小時之後再試' )
            var = '密碼輸入錯誤超過3次,該賬號已被鎖定,請' + str(dateline - (t2 - t1))  + '小時之後再試'
            tkMessageBox.showwarning('警告', var )
        if (dateline - (t2 - t1)).days < 0:
            count  = 0
            print (u'賬號鎖定已解除,請輸入正確的賬號密碼!')
            tkMessageBox.showwarning('警告','賬號鎖定已解除,請輸入正確的賬號密碼!')
            
    else:
        if username == '' and password == '':
            tkMessageBox.showinfo('警告','賬號和密碼不能為空')
        elif username == '' and password != '':
            tkMessageBox.showinfo('警告','賬號不能為空')
        elif username != '' and password == '':
            tkMessageBox.showinfo('警告','密碼不能為空')   
        else:       
            if  username == 'Navy' and password == '9527':
                print(u'歡迎 Navy')
                window.destroy()
                tkMessageBox.showinfo('主頁面','歡迎 Navy')

            else:
                
                count += 1
                
                if count == 1:
                    print(u'密碼輸入錯誤,請重新輸入!')
                    print('再輸錯%d次該賬號將被鎖定' % (3-count))
                    tkMessageBox.showwarning('警告','密碼輸入錯誤,請重新輸入!再輸錯2次該賬號將被鎖定!')

                if count == 2:
                    print(u'密碼輸入錯誤,請重新輸入!')
                    print('再輸錯%d次該賬號將被鎖定' % (3-count))
                    tkMessageBox.showwarning('警告','密碼輸入錯誤,請重新輸入!再輸錯1次該賬號將被鎖定!')
                    
                if count == 3:
                    t1 = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
                    t1= datetime.datetime.strptime(t1,'%Y-%m-%d %H:%M:%S')
                    print(u'密碼輸入錯誤超過3次,該賬號已被鎖定,請24小時之後再試')
                    tkMessageBox.showwarning('警告','密碼輸入錯誤超過3次,該賬號已被鎖定,請24小時之後再試')
                    count = count + 1
              
if __name__=='__main__':
    
    window = tk.Tk()
    window.title('個人網上銀行登入')#標題
    window.geometry('320x200')#窗體大小
    window.resizable(False, False)#固定窗體

    frame1 = tk.Frame(window)
    frame2 = tk.Frame(window)
    frame3 = tk.Frame(window)

    userNameLabel = tk.Label(frame1,text="賬號:",fg = 'green')
    userNameLabel.pack(side = tk.LEFT)
    
    passWordLabel = tk.Label(frame2,text="密碼:",fg='green')
    passWordLabel.pack(side = tk.LEFT)
    
    entry = tk.Entry(frame1)
    entry.pack(side=tk.RIGHT)    
    
    entry2 = tk.Entry(frame2,show = '*')
    entry2.pack(side=tk.RIGHT)  
    
    # 通過中介函式handlerAdapotor進行command設定
    btn = tk.Button(frame3,text = u'安全登入', command = handler,fg = 'blue')
    btn.pack(side=tk.BOTTOM)

    
    time1 = ''
    clock = tk.Label(window,font=('times', 10, 'bold'),fg = 'blue')
    clock.pack(side=tk.BOTTOM) 
    tick()
    
    frame1.pack(fill = tk.Y, expand = tk.YES)
    frame2.pack(fill = tk.Y, expand = tk.NO)
    frame3.pack(fill = tk.NONE, expand = tk.YES)
    window.mainloop()
code 下載,請點選UI登入介面 在該連結下載!