Python·Tkinter——用Entry写个登录器界面(输入栏含提示语)

用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('<Button-1>',Account_Blank)
entry_password.bind('<Button-1>',Password_Blank)
button_login.bind('<Button-1>',Login)


# 进入消息循环,保持程序持续运行
root.mainloop()

运行下看看效果:

  • 开始界面:
    在这里插入图片描述
  • 登陆错误:
    在这里插入图片描述
  • 登录成功:
    在这里插入图片描述

输入栏的提示语的写法会导致,当输入账号和密码时,想去改已经输入的部分,会直接清除从头开始。
但是我想不到更好的方式让输入栏显示提示语,欢迎大佬提点建议。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值