【界面设计】初识Tkinter,教你用Python设计一个简单的登录系统

简介: Python简单的登录系统

前言
需要源码自取:https://download.csdn.net/download/m0_68111267/87369964?spm=1001.2014.3001.5503
登陆系统.png
一、初识Tkinter
Tkinter是Python中有关界面设计的一个包,使用起来非常简单,学习起来也非常快,在Python3中可以直接使用,下面我将介绍一些简单的函数
1.创建界面

import tkinter as tk
root=tk.Tk()
root.title('欢迎来到tkinter')
root.geometry('500x300')
root.mainloop() 

创建了一个宽为500,高为300的界面,界面标题为“欢迎来到tkinter” 。
2.Label控件

import tkinter as tk
root=tk.Tk()
root.title('欢迎来到tkinter')
root.geometry('500x300')
l=tk.Label(root,text='欢迎光临Tkinter',font=('宋体',12),bg='white',fg='black',width=20,height=2)
l.pack()
root.mainloop() 

在root界面中创建了一个宽为20,高为2的Label标签,标签中写入了“欢迎光临Tkinter”,背景为白色,字体为宋体,大小为12号,颜色为黑色。
3.Button控件

import tkinter as tk
root=tk.Tk()
root.title('欢迎来到tkinter')
root.geometry('500x300')
var=tk.StringVar()
var.set('Welcome')
l=tk.Label(root,text='欢迎光临Tkinter',bg='white',fg='black',width=20,height=2)
l.pack()
ls=tk.Label(root,textvariable=var,bg='black',fg='white',width=20,height=2)
ls.pack()
def hello():
    var.set('Hello')
b=tk.Button(root,text='点击',bg='blue',fg='white',command=hello)
b.pack()
root.mainloop()

在root界面中创建了一个“点击”按钮,背景为蓝色,字体为白色,按钮的方法为hello。
4.Entry控件

import tkinter as tk
root=tk.Tk()
root.title('欢迎来到tkinter')
root.geometry('500x300')
var=tk.StringVar()
var.set('Welcome')
l=tk.Label(root,text='欢迎光临Tkinter',bg='white',fg='black',width=20,height=2)
l.pack()
ls=tk.Label(root,textvariable=var,bg='black',fg='white',width=20,height=2)
ls.pack()
def hello():
    var.set('Hello')
b=tk.Button(root,text='点击',bg='blue',fg='white',command=hello)
b.pack()
entry=tk.StringVar()
e=tk.Entry(root,textvariable=entry,font=('宋体',12),bg='red',fg='white')
e.pack()
root.mainloop()

在root界面中创建了一个文本框,背景为红色,输入字体为白色。
二、登录系统
一个简单的登录系统包含了登录账户、注册账户、修改密码以及注销账户的操作。
1.登录账户
登录系统主要需要判断账户是否存在,不存在就注册一个账户,如果第一次登录系统,我们需要先新建一个文件,保存管理员的账户以及密码,具体代码如下:

def user_login():
    user_name=name.get()
    user_key=key.get()
    try:
        with open('d:\\user.pickle','rb') as user_file:
            user_info=pickle.load(user_file)
    except FileNotFoundError:
        with open('d:\\user.pickle','wb') as user_file:
            user_info={'admin':'12345'}
            pickle.dump(user_info,user_file)
            user_file.close()
    if user_name in user_info:
        if user_key == user_info[user_name]:
            tkinter.messagebox.showinfo(title='提示', message='登录成功!' )
        else:
            tkinter.messagebox.showerror('提示','密码错误!请重新输入')
    else:  
        exist = tkinter.messagebox.askyesno('提示','该用户名未注册,是否立即注册?')
        if exist:
            user_sign()

2.注册账户
注册账户时需要判断该账户是否存在,如果已经存在时提醒用户重新注册即可。
注册时还要输入两次密码,判断是否一致。
具体代码如下:

def user_sign():
    sign_root=tk.Toplevel(root)
    sign_root.title('注册账户')
    screenheight=sign_root.winfo_screenheight()
    screenwidth=sign_root.winfo_screenwidth()
    h=220
    w=300
    x=(screenwidth-w)//2
    y=(screenheight-h)//2
    sign_root.geometry("%dx%d+%d+%d"%(w,h,x,y))
    new_name=tk.StringVar()
    tk.Label(sign_root,text='欢迎来到注册系统',font=('宋体',20),bg='white',fg='red',width=20,height=1).place(x=7,y=10)
    tk.Label(sign_root,text='新的账户',font=('宋体',12)).place(x=20,y=60)
    entry_name=tk.Entry(sign_root,textvariable=new_name,font=('宋体',12),show=None).place(x=100,y=60)
    new_key=tk.StringVar()
    tk.Label(sign_root,text='新的密码',font=('宋体',12)).place(x=20,y=100)
    entry_key=tk.Entry(sign_root,textvariable=new_key,font=('宋体',12),show='*').place(x=100,y=100)
    same_key=tk.StringVar()
    tk.Label(sign_root,text='请确认密码',font=('宋体',12)).place(x=10,y=140)
    entry_keys=tk.Entry(sign_root,textvariable=same_key,font=('宋体',12),show='*').place(x=100,y=140)
    def sign_check():
        name=new_name.get()
        key1=new_key.get()
        key2=same_key.get()
        with open('d:\\user.pickle','rb') as user_file:
            user_info=pickle.load(user_file)
            user_file.close()
        if name in user_info:
            tkinter.messagebox.showerror('提示','该账户已存在!')
        elif key1!=key2:
            tkinter.messagebox.showerror('提示','两次输入密码不一致,请重新输入!')
        else:
            user_info[name]=key1
            with open(r'd:\\user.pickle','wb') as user_file:
                pickle.dump(user_info,user_file)
                user_file.close()
            tkinter.messagebox.showinfo('提示','注册成功!')
            sign_root.destroy()
    tk.Button(sign_root,text='注册',font=('宋体',12),bg='red',fg='white',width=5,height=1,command=sign_check).place(x=130,y=180)    

3.修改密码
修改密码时需要先确定需要修改密码的账户是否存在,不存在无法修改密码。
修改密码时如果账户存在,还要判断原密码是否一致以及新输入的两次密码是否一致。
具体源码如下:

def user_change():
    change_root=tk.Toplevel(root)
    old_name=tk.StringVar()
    old_key=tk.StringVar()
    new_key=tk.StringVar()
    same_key=tk.StringVar()
    change_root.title('修改密码')
    screenheight=change_root.winfo_screenheight()
    screenwidth=change_root.winfo_screenwidth()
    h=220
    w=300
    x=(screenwidth-w)//2
    y=(screenheight-h)//2
    change_root.geometry("%dx%d+%d+%d"%(w,h,x,y))
    tk.Label(change_root,text='欢迎来到修改密码系统',font=('宋体',20),bg='white',fg='green',width=20,height=1).place(x=5,y=10)
    tk.Label(change_root,text='请输入你的账号',font=('宋体',12),width=15,height=1).place(x=5,y=60)
    tk.Entry(change_root,textvariable=old_name,show=None).place(x=130,y=60)
    tk.Label(change_root,text='请输入原始密码',font=('宋体',12),width=15,height=1).place(x=5,y=90)
    tk.Entry(change_root,textvariable=old_key,show=None).place(x=130,y=90)
    tk.Label(change_root,text='请输入修改密码',font=('宋体',12),width=15,height=1).place(x=5,y=120)
    tk.Entry(change_root,textvariable=new_key,show='*').place(x=130,y=120)
    tk.Label(change_root,text='请确认你的密码',font=('宋体',12),width=15,height=1).place(x=5,y=150)
    tk.Entry(change_root,textvariable=same_key,show='*').place(x=130,y=150)
    def change_check():
        name=old_name.get()
        key1=old_key.get()
        key2=new_key.get()
        key3=same_key.get()
        with open("d:\\user.pickle",'rb') as user_file:
            user_info=pickle.load(user_file)
            user_file.close()
        if name in user_info:
            if key1==user_info[name]:
                if key2==key3:
                    user_info[name]=key2
                    with open('d:\\user.pickle','wb') as user_file:
                        pickle.dump(user_info,user_file)
                        user_file.close()
                    tkinter.messagebox.showinfo('提示',"修改成功!")
                    change_root.destroy()
                else:
                    tkinter.messagebox.showerror('提示','两次密码不一致,请重新输入!')
            else:
                tkinter.messagebox.showerror('提示','密码错误,请重新输入!')
        else:
            exist=tkinter.messagebox.askyesno('提示','该账户不存在,是否立即注册账户?')
            if exist:
                user_sign()      
    tk.Button(change_root,text='修改',font=('宋体',12),bg='green',fg='white',command=change_check).place(x=130,y=180) 

4.注销账户
注销账户时需要先判断账户是否存在,然后输入两次原密码,当输入的两次原密码相同时即可注销成功,具体代码如下:

def user_del():
    old_name=tk.StringVar()
    old_key=tk.StringVar()
    same_key=tk.StringVar()
    del_root=tk.Toplevel(root)
    del_root.title('注销账户')
    screenheight=del_root.winfo_screenheight()
    screenwidth=del_root.winfo_screenwidth()
    h=220
    w=300
    x=(screenwidth-w)//2
    y=(screenheight-h)//2
    del_root.geometry("%dx%d+%d+%d"%(w,h,x,y))
    tk.Label(del_root,text='欢迎来到注销账户系统',font=('宋体',20),bg='white',fg='black',width=20,height=1).place(x=5,y=10)
    tk.Label(del_root,text='请输入你的账号',font=('宋体',12),width=15,height=1).place(x=5,y=60)
    tk.Entry(del_root,textvariable=old_name,show=None).place(x=130,y=60)
    tk.Label(del_root,text='请输入原始密码',font=('宋体',12),width=15,height=1).place(x=5,y=100)
    tk.Entry(del_root,textvariable=old_key,show=None).place(x=130,y=100)
    tk.Label(del_root,text='请确认你的密码',font=('宋体',12),width=15,height=1).place(x=5,y=140)
    tk.Entry(del_root,textvariable=same_key,show='*').place(x=130,y=140)    
    def del_check():
        name=old_name.get()
        key1=old_key.get()
        key2=same_key.get()
        with open('d:\\user.pickle','rb') as user_file:
            user_info=pickle.load(user_file)
            user_file.close()
        if name in user_info:
            if key1==key2:
                if key1==user_info[name]:
                    user_info.pop(name)
                    with open('d:\\user.pickle','wb') as user_file:
                        pickle.dump(user_info,user_file)
                        user_file.close()
                    tkinter.messagebox.showinfo('提示','注销成功!')
                else:
                    tkinter.messagebox.showerror('提示','密码错误!')
            else:
                tkinter.messagebox.showerror('提示','两次密码不一致!')
        else:
            exist=tkinter.messagebox.askyesno('提示','该账户不存在,是否立即注册?')
            if exist:
                user_sign()
    tk.Button(del_root,text='注销',font=('宋体',12),bg='black',fg='white',command=del_check).place(x=130,y=180)  
目录
相关文章
|
2月前
|
算法 搜索推荐 JavaScript
基于python智能推荐算法的全屋定制系统
本研究聚焦基于智能推荐算法的全屋定制平台网站设计,旨在解决消费者在个性化定制中面临的选择难题。通过整合Django、Vue、Python与MySQL等技术,构建集家装设计、材料推荐、家具搭配于一体的一站式智能服务平台,提升用户体验与行业数字化水平。
|
2月前
|
存储 分布式计算 大数据
基于Python大数据的的电商用户行为分析系统
本系统基于Django、Scrapy与Hadoop技术,构建电商用户行为分析平台。通过爬取与处理海量用户数据,实现行为追踪、偏好分析与个性化推荐,助力企业提升营销精准度与用户体验,推动电商智能化发展。
|
2月前
|
机器学习/深度学习 大数据 关系型数据库
基于python大数据的台风灾害分析及预测系统
针对台风灾害预警滞后、精度不足等问题,本研究基于Python与大数据技术,构建多源数据融合的台风预测系统。利用机器学习提升路径与强度预测准确率,结合Django框架实现动态可视化与实时预警,为防灾决策提供科学支持,显著提高应急响应效率,具有重要社会经济价值。
|
2月前
|
机器学习/深度学习 大数据 关系型数据库
基于python大数据的青少年网络使用情况分析及预测系统
本研究基于Python大数据技术,构建青少年网络行为分析系统,旨在破解现有防沉迷模式下用户画像模糊、预警滞后等难题。通过整合多平台亿级数据,运用机器学习实现精准行为预测与实时干预,推动数字治理向“数据驱动”转型,为家庭、学校及政府提供科学决策支持,助力青少年健康上网。
|
3月前
|
数据采集 数据可视化 关系型数据库
基于python大数据的电影数据可视化分析系统
电影分析与可视化平台顺应电影产业数字化趋势,整合大数据处理、人工智能与Web技术,实现电影数据的采集、分析与可视化展示。平台支持票房、评分、观众行为等多维度分析,助力行业洞察与决策,同时提供互动界面,增强观众对电影文化的理解。技术上依托Python、MySQL、Flask、HTML等构建,融合数据采集与AI分析,提升电影行业的数据应用能力。
基于python的餐厅点餐系统
本课题研究开发餐厅点餐系统,旨在提升餐厅信息处理效率与管理水平。通过计算机技术规范点餐流程,加快信息处理速度,助力管理人员高效运作。系统包含功能结构图与具体实现模块,全面展示系统设计与运行逻辑。

推荐镜像

更多