收藏文章 楼主

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

版块:AIGC   类型:普通   作者:AI绘图   查看:103   回复:0   获赞:0   时间:2023-11-01 03:30:52

最近在学习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()

运行下看看效果:

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

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

 
回复列表
默认   热门   正序   倒序

回复:Python·Tkinter·学习记录——用Entry写个登录器界面(输入栏含提示语)

Powered by 7.12.10

©2015 - 2025 90Link

90link品牌推广 网站地图

您的IP:10.1.228.218,2025-12-21 17:15:49,Processed in 0.27683 second(s).

豫ICP备2023005541号

头像

用户名:

粉丝数:

签名:

资料 关注 好友 消息
免责声明
  • 1、本网站所刊载的文章,不代表本网站赞同其观点和对其真实性负责,仅供参考和借鉴。
  • 2、文章中的图片和文字版权归原作者所有,如有侵权请及时联系我们,我们将尽快处理。
  • 3、文章中提到的任何产品或服务,本网站不对其性能、质量、适用性、可靠性、安全性、法律合规性等方面做出任何保证或承诺,仅供读者参考,使用者自行承担风险。
  • 4、本网站不承担任何因使用本站提供的信息、服务或产品而产生的直接、间接、附带或衍生的损失或责任,使用者应自行承担一切风险。

侵权删除请致信 E-Mail:3454251265@qq.com