实验目的:
使用Python的Tkinter库制作一个简单的用户登录界面,在输入用户名以及其密码后需判断是否用户名与密码匹配,然后做出相应的提示。
实验内容:
先是建立文本文件里面储存用户名以及密码。在用户输入用户名以及密码,通过文件中的用户名以及密码进行判断匹配,看是否一致。如果一致那么弹出新的窗口,显示登录成功。若不一致,则弹出错误。点击OK,重新返回到登录界面。
1. 导入tkinter库
Tkinter是python默认的GUI库,无需下载,直接导入就行。
import tkinter
2. 读取文件
形成用户名与密码所对应的字典。为后面判断用户名与密码是否一致做准备。
f = open(', 'r', encoding='utf-8')
fi = f.readlines()
dic = {}
for lst in fi:
d = str(lst).strip('n').split(",")
print(d)
dic[d[0]] = d[1]
3. 设计主窗口
创建应用窗口,并且命名相应的窗口标题。以及设置窗口大小。
# 创建应用程序
root = tkinter.Tk()
# 设置窗口的标题
root.title('用户登录')root['height'] = 300
root['width'] = 600
4. 设置窗口要素
利用label标签组件创建,标题,用户名以及密码。用place函数确定组件位置。其坐标位置是通过挨个试错,最后确定最后坐标。
labelTitle = tkinter.Label(root, text='请输入用户名和密码', font=("微软雅黑", 14), justify=tkinter.CENTER, anchor='center',
width=200)
labelTitle.place(x=210, y=15, width=200, height=30)
labeName = tkinter.Label(root, text='用户名:', font=("微软雅黑", 14), justify=tkinter.RIGHT, anchor='e', width=80)
# 显示该组件的位置及大小
labeName.place(x=50, y=50, width=125, height=30)labeName = tkinter.Label(root, text='密码:', font=("微软雅黑", 14), justify=tkinter.RIGHT, anchor='e', width=80)
# 显示该组件的位置及大小
labeName.place(x=50, y=90, width=125, height=30)
使用两组Label和Entry的组合来实现保存用户输入的文本,tkinter库中的StringVar用来创建变量空间,结合Entry输入框可以讲用户输入的字符串保存到变量空间中,使用get()可以获取其中的内容,这里同意通过place函数来移动。
# 创建字符串变量和文本框组件,同时设置关联的变量
varName = tkinter.StringVar(root, value='')
entryName = tkinter.Entry(root, width=80, textvariable=varName)
entryName.place(x=200, y=50, width=250, height=30)# 创建密码文本框,同时设置关联的变量
varPwd = tkinter.StringVar(root, value='')
entryPwd = tkinter.Entry(root, show='*', width=80, textvariable=varPwd)
entryPwd.place(x=200, y=90, width=250, height=30)
设置登录和取消按钮。两个button按钮,点击确定按钮检查用户输入内容。并且编写登录与取消对应函数。
登录函数通过变量获取获取输入的用户名以及密码。通过判断函数在字典中对比用户名与密码是否存在和一致。若存在,将窗口拉长,显示出新的提示信息,以及增加新的按钮Ok。并且防止bug,再点击登录键后,将先禁止按钮使用。只有点击OK后才能重新点击登录与取消键。如果不存在增加新的提示信息。
在函数中再嵌套一个子函数。作为OK键的关联函数。利用destory函数摧毁登陆后新增加的组件,回到登陆原始状态。
def OK():
root['height'] = 300
labelPrompt.destroy() # 删除控件
labelMessage.destroy()
buttonOk['state'] = 'active' # 将按钮改为可用状态
buttonCancel['state'] = 'active'
if name in dic and dic[name] == pwd:
labelMessage = tkinter.Label(root, text='Message', fg='white', bg='blue', font=("Times New Roman", 16),
justify=tkinter.RIGHT, anchor='w', width=80)
labelMessage.place(x=0, y=200, width=600, height=30)
root['height'] = 350
labelPrompt = tkinter.Label(root, text='登陆成功', font=("微软雅黑", 14), justify=tkinter.RIGHT, anchor='e', width=80)
labelPrompt.place(x=90, y=250, width=90, height=30)
buttonOkk = tkinter.Button(root, text='ok', font=("微软雅黑", 14), activeforeground='#ff0000', command=OK)
buttonOkk.place(x=500, y=300, width=80, height=30)
else:
labelMessage = tkinter.Label(root, text="Message "
" X ", fg='white', bg='blue',
font=("Times New Roman", 16),
justify=tkinter.RIGHT, anchor='w', width=600)
labelMessage.place(x=0, y=200, width=600, height=30)
root['height'] = 350
labelPrompt = tkinter.Label(root, text='用户名和密码不匹配', font=("微软雅黑", 14), justify=tkinter.RIGHT, anchor='w',
width=100)
labelPrompt.place(x=90, y=250, width=180, height=30)
buttonOkk = tkinter.Button(root, text='ok', font=("微软雅黑", 14), activeforeground='#ff0000', command=OK)
buttonOkk.place(x=500, y=300, width=80, height=30)
创建取消函数连接函数。
# 取消按钮的事件处理函数
def cancel():
# 清空用户输入的用户名和密码
varName.set('')
varPwd.set('')
最后创建登录以及取消按钮将函数与其关联
# 创建按钮组件,同时设置按钮事件处理函数
# 参数解释: text='Login'文本内容 activeforeground='#ff0000'按下按钮时文字颜色 command=login关联的函数
buttonOk = tkinter.Button(root, text='确定', command=login)
buttonOk.place(x=200, y=135, width=80, height=30)buttonCancel = tkinter.Button(root, text='取消', command=cancel)
buttonCancel.place(x=370, y=135, width=80, height=30)
5. 然后设置消息循环
# 启动消息循环
root.mainloop()
项目总结:
这个项目对于tkinter入门是十分友好的。利用这个项目极大熟悉相关组件的使用,以及函数的编写。在项目开始时,由于对相关内容的不熟悉,此进度缓慢。对于相关组件的位置进行了长时间的调整。此项目对于本人的整体知识掌握具有显著作用。
最后整合整体代码呈现如下:
import tkinter
f = open(', 'r', encoding='utf-8')
fi = f.readlines()
dic = {}
for lst in fi:
d = str(lst).strip('n').split(",")
print(d)
dic[d[0]] = d[1]
# 创建应用程序
root = tkinter.Tk()
# 设置窗口的标题
root.title('用户登录')
# 设置窗口大小
root['height'] = 300
root['width'] = 600
# 在窗口上创建标签组件(User Name) 各个参数的解释: text设置文本内容 fg='设置字体颜色' bg='设置字体背景' font=("设置字体",设置字体大小)
# justify=文本标签对齐的方式 anchor='文本对其方式', width=设置的宽度
labelTitle = tkinter.Label(root, text='请输入用户名和密码', font=("微软雅黑", 14), justify=tkinter.CENTER, anchor='center',
width=200)
labelTitle.place(x=210, y=15, width=200, height=30)
labeName = tkinter.Label(root, text='用户名:', font=("微软雅黑", 14), justify=tkinter.RIGHT, anchor='e', width=80)
# 显示该组件的位置及大小
labeName.place(x=50, y=50, width=125, height=30)
# 创建字符串变量和文本框组件,同时设置关联的变量
varName = tkinter.StringVar(root, value='')
entryName = tkinter.Entry(root, width=80, textvariable=varName)
entryName.place(x=200, y=50, width=250, height=30)
# 在窗口上创建标签组件(User Pwd)
labeName = tkinter.Label(root, text='密码:', font=("微软雅黑", 14), justify=tkinter.RIGHT, anchor='e', width=80)
# 显示该组件的位置及大小
labeName.place(x=50, y=90, width=125, height=30)
# 创建密码文本框,同时设置关联的变量
varPwd = tkinter.StringVar(root, value='')
entryPwd = tkinter.Entry(root, show='*', width=80, textvariable=varPwd)
entryPwd.place(x=200, y=90, width=250, height=30)
# 登录按钮事件处理函数
def login():
# 获取用户名和密码
buttonCancel['state'] = 'disable'
buttonOk['state'] = 'disable'
name = ()
pwd = ()
# OK按钮事件处理函数
def OK():
root['height'] = 300
labelPrompt.destroy() # 删除控件
labelMessage.destroy()
buttonOk['state'] = 'active' # 将按钮改为可用状态
buttonCancel['state'] = 'active'
if name in dic and dic[name] == pwd:
labelMessage = tkinter.Label(root, text='Message', fg='white', bg='blue', font=("Times New Roman", 16),
justify=tkinter.RIGHT, anchor='w', width=80)
labelMessage.place(x=0, y=200, width=600, height=30)
root['height'] = 350
labelPrompt = tkinter.Label(root, text='登陆成功', font=("微软雅黑", 14), justify=tkinter.RIGHT, anchor='e', width=80)
labelPrompt.place(x=90, y=250, width=90, height=30)
buttonOkk = tkinter.Button(root, text='ok', font=("微软雅黑", 14), activeforeground='#ff0000', command=OK)
buttonOkk.place(x=500, y=300, width=80, height=30)
else:
labelMessage = tkinter.Label(root, text="Message "
" X ", fg='white', bg='blue',
font=("Times New Roman", 16),
justify=tkinter.RIGHT, anchor='w', width=600)
labelMessage.place(x=0, y=200, width=600, height=30)
root['height'] = 350
labelPrompt = tkinter.Label(root, text='用户名和密码不匹配', font=("微软雅黑", 14), justify=tkinter.RIGHT, anchor='w',
width=100)
labelPrompt.place(x=90, y=250, width=180, height=30)
buttonOkk = tkinter.Button(root, text='ok', font=("微软雅黑", 14), activeforeground='#ff0000', command=OK)
buttonOkk.place(x=500, y=300, width=80, height=30)
# 创建按钮组件,同时设置按钮事件处理函数
# 参数解释: text='Login'文本内容 activeforeground='#ff0000'按下按钮时文字颜色 command=login关联的函数
buttonOk = tkinter.Button(root, text='确定', command=login)
buttonOk.place(x=200, y=135, width=80, height=30)
# 取消按钮的事件处理函数
def cancel():
# 清空用户输入的用户名和密码
varName.set('')
varPwd.set('')
buttonCancel = tkinter.Button(root, text='取消', command=cancel)
buttonCancel.place(x=370, y=135, width=80, height=30)
# 启动消息循环
root.mainloop() 作者:余执一WY 出处:bilibili
本文发布于:2024-02-05 03:54:24,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170723456962840.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |