import tkinter
import tkinter.messagebox
import os
import os.path
path=os.getenv('temp')
filename=os.path.join(path,'info.txt')
#创建应用程序窗口
root=tkinter.Tk()
root['height']=140
root['width']=200
#在窗口上创建标签组件
labelname=tkinter.Label(root,text='user name:',justify=tkinter.RIGHT,anchor='e',width=80)
#把组件放到窗口上的指定区域
labelname.place(x=10,y=5,width=80,height=20)
varname=tkinter.StringVar(root,value='')
entryname=tkinter.Entry(root,width=80,textvariable=varname)
entryname.place(x=100,y=5,width=80,height=20)
labelpwd=tkinter.Label(root,text='user pwd:',justify=tkinter.RIGHT,anchor='e',width=80)
labelpwd.place(x=10,y=30,width=80,height=20)
#创建密码本文框
varpwd=tkinter.StringVar(root,value=' ')
entrypwd=tkinter.Entry(root,show='*',width=80,textvariable=varpwd)
entrypwd.place(x=100,y=30,width=80,height=20)
#尝试自动填写用户和密码
try:
with open(filename)as fp:
n,p=fp.read().strip().split(',')
varname.set(n)
varpwd.set(p)
except:
pass
#复选框
rememberme=tkinter.IntVar(root,value=1)
#选中为1未选中为0,默认选中
checkremember=tkinter.Checkbutton(root,text='remember me?',variable=rememberme,onvalue=1,offvalue=0)
checkremember.place(x=30,y=70,width=120,height=20)
#登陆按钮事件处理
def login():
name=entryname.get()
pwd=entrypwd.get()
if name=='admin' and pwd=='123456':
tkinter.messagebox.showinfo(title='恭喜',message='登陆成功')
if rememberme.get()==1:
with open(filename,'w') as fp:
fp.write(','.join((name,pwd)))
else:
try:
os.remove(filename)
except: pass
else:
tkinter.messagebox.showerror('警告',message='用户名或密码错误')
buttonok=tkinter.Button(root,text='登陆',command=login)
buttonok.place(x=30,y=100,width=50,height=20)
def cancel():
varpwd.set('')
varname.set('')
buttonCancel=tkinter.Button(root,text='删除',command=cancel)
buttonCancel.place(x=90,y=100,width=50,height=20)
root.mainloop()
使用Python编写模拟用户登录界面
最新推荐文章于 2024-07-04 17:36:25 发布