python_ocr文字識別小工具 1.2 (TKinter得學習使用之案例——模擬登錄)

python_ocr文字識別小工具 1.2 (TKinter得學習使用之案例——模擬登錄)

代碼

import tkinter
import tkinter.messagebox

# 問題1:講控件加入窗口方式:place和pack等得區別
# 問題2:創建tk變量

# 創建應用程序窗口
root = tkinter.Tk()
# 在窗口上創建標籤組件
lableName = tkinter.Label(root,text='User Name',justify = tkinter.RIGHT,width = 80)
lableName.place(x=10 , y=5 ,width = 80 ,height = 20)
#創建字符串變量和文本框組件,同時設置關聯得變量
varName = tkinter.StringVar(root , value='')
entryName = tkinter.Entry(root , width =80 , textvariable = varName)
entryName.place(x = 100 , y = 5 , width = 80 ,height = 20)
lablePwd = tkinter.Label(root, text = 'User Pwd:' , justify = tkinter.RIGHT,width = 80)
lableName.place(x=10,y=30,width=80,height=20)
#創建密碼文本框
varPwd = tkinter.StringVar(root , value = '')
entryPwd = tkinter.Entry(root,show='*' , width = 80 , textvariable = varPwd)
entryPwd.place(x = 100,y=30,width = 80,height = 20)
# 登錄按鈕事件處理函數
def login():
    name = entryName.get()
    pwd = entryPwd.get()
    if name == 'admin' and pwd == '123456':
        tkinter.messagebox.showinfo(title='恭喜' , message = '登陸成功!')
    else:
        tkinter.messagebox.showerror('警告',message='用戶名或密碼錯誤')
# 創建按鈕組件,同時設置按鈕事件處理函數
buttonOk = tkinter.Button(root,text='Login',command=login)
buttonOk.place(x=30,y=70,width=50,height=20)
# 取消按鈕得事件處理函數
def cancel():
    #清空用戶輸入得用戶名和密碼
    varName.set('')
    varPwd.set('')
buttonCancel = tkinter.Button(root,text='Cancel',command=cancel)
buttonCancel.place(x=90,y=70,width=50,height=20)
#啓動消息循環
root.mainloop()

實現效果:

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章