最近在学习Tkinter,写个简单的登录器界面作为学习记录。
import tkinter as tk
root = tk.Tk()
root.geometry('200x200')
Members=[
{
'account':'张三','password':'333'},
{
'account':'李四','password':'444'},
{
'account':'王二麻子','password':'mazi'}]
""" * * * * * * * * * * * * * * * *
* 编 写 功 能 函 数 *
* * * * * * * * * * * * * * * *
"""
def validate_func_account():
if entry_account.get():
return True
else:
return False
def invalidate_func_account():
entry_account.insert(0,"账号")
return True
def validate_func_password():
if entry_password.get():
return True
else:
return False
def invalidate_func_password():
entry_password.insert(0,"密码")
return True
""" * * * * * * * * * * * * * * *
* 创 建 控 件 *
* * * * * * * * * * * * * * *
"""
def Entry_Account(window):
entry_account = tk.Entry(window,validate='focusout',validatecommand=validate_func_account,invalidcommand=invalidate_func_account)
entry_account.insert(0,"账号")
entry_account.pack()
return entry_account
def Entry_Password(window):
entry_password = tk.Entry(window,
validate='focusout',validatecommand=validate_func_password,invalidcommand=invalidate_func_password)
entry_password.insert(0,"密码")
entry_password.pack()
return entry_password
def Button_Login(window):
button_login = tk.Button(window,text='登录')
button_login.place(anchor='center',relx=0.5,rely=0.5,width=100,height=60)
return button_login
def Label_login_success(window):
label_login_success = tk.Label(window,text='登录成功',bg='#F0FFF0')
label_login_success.place(anchor='s',relx=0.5,rely=1,width=100,height=60)
return label_login_success
def Label_login_failure_a(window):
label_login_failure_a = tk.Label(window,text='账号错误',bg='#F0FFF0')
label_login_failure_a.place(anchor='s',relx=0.5,rely=1,width=100,height=60)
return label_login_failure_a
def Label_login_failure_p(window):
label_login_failure_p = tk.Label(window,text='密码错误',bg='#F0FFF0')
label_login_failure_p.place(anchor='s',relx=0.5,rely=1,width=100,height=60)
return label_login_failure_p
""" * * * * * * * * * * * * * * * *
* 添 加 控 件 *
* * * * * * * * * * * * * * * *
"""
entry_account = Entry_Account(root)
entry_password = Entry_Password(root)
button_login = Button_Login(root)
""" * * * * * * * * * * * * * * * *
* 编 写 事 件 *
* * * * * * * * * * * * * * * *
"""
def Account_Blank(event=True):
entry_account.delete(0, "end")
pass
def Password_Blank(event=True):
entry_password.delete(0, "end")
pass
def Login(event=True):
for member in Members:
if entry_account.get() == member['account']:
account = 1
if entry_password.get() == member['password']:
password = 1
else:
password = 0
else:
pass
if account == 1 and password==1:
Label_login_success(root)
elif account == 0:
Label_login_failure_a(root)
elif password == 0:
Label_login_failure_p(root)
""" * * * * * * * * * * * * * * * *
* 绑 定 事 件 *
* * * * * * * * * * * * * * * *
"""
entry_account.bind('' ,Account_Blank)
entry_password.bind('' ,Password_Blank)
button_login.bind('' ,Login)
# 进入消息循环,保持程序持续运行
root.mainloop()
运行下看看效果:
输入栏的提示语的写法会导致,当输入账号和密码时,想去改已经输入的部分,会直接清除从头开始。
但是我想不到更好的方式让输入栏显示提示语,欢迎大佬提点建议。