这次的Python Tkinter窗体登录程序模板还是之前的,在此基础上进行了改进,这一次是通过创建一个新的列表和字典来存储和验证账号和密文(加密后的账号和密码);第一次的窗体登录程序是通过连接SQL数据库来存储账号和密码。
这是我自己将其封装后的代码,控件的位置是用相对位置表示,只能调用类里面的主函数Main()。
import hashlib
import sys
import tkinter
import tkinter.messagebox
from tkinter import *
class FForm:
# 设置账号密码存储
def __init__(self):
self.__List = []
self.__Dic = dict()
# 登录界面重置1
def __Clear1(self, *args):
self.__text1.delete(0, tkinter.END)
self.__text2.delete(0, tkinter.END)
self.__text1.focus()
# 注册界面重置3
def __Clear3(self, *args):
self.__text11.delete(0, tkinter.END)
self.__text12.delete(0, tkinter.END)
self.__text13.delete(0, tkinter.END)
self.__text11.focus()
# 修改界面重置4
def __Clear4(self):
self.__text32.delete(0, tkinter.END)
self.__text33.delete(0, tkinter.END)
self.__text32.focus()
# 注销界面重置5
def __Clear5(self):
self.__text41.delete(0, tkinter.END)
self.__text41.focus()
# 退出
def __Exit(self, *args):
self.__Form1.quit()
self.__Form1.destroy() # 关闭窗体·Form1后台运行
self.__Form2.quit()
self.__Form2.destroy()
self.__Form3.quit()
self.__Form3.destroy()
self.__Form4.quit()
self.__Form4.destroy()
self.__Form5.quit()
self.__Form5.destroy()
sys.exit(1)
# 窗体居中
def __Center(self, winform):
winform.config(background="#C0C0C0")
self.__width = winform.winfo_screenwidth() # 获取屏幕宽度
self.__height = winform.winfo_screenheight() # 获取屏幕高度
winform.resizable(False, False) # 窗体固定尺寸
winform.geometry("%dx%d+%d+%d" % (self.__width / 2.1, self.__height / 2.1, self.__width / 4.0, self.__height / 4.0))
winform.protocol("WM_DELETE_WINDOW", self.__Exit)
# 跳转指定界面窗体
def __Form(self, st):
if st == 1:
# 在跳转的窗体的时候要用withdraw()来隐藏当前窗体,不能使用destroy()来关闭窗体,
# 因为会断掉与其他窗体的数据关联,特别是在连接了数据库的情况下。
self.__Clear3()
self.__Form2.withdraw() # 隐藏窗体Form2
self.__Form3.withdraw() # 隐藏窗体Form2
self.__Form4.withdraw() # 隐藏窗体Form2
self.__Form5.withdraw() # 隐藏窗体Form2
self.__Center(self.__Form1) # 窗体Form1居中显示
self.__Form1.deiconify() # 窗体Form1显示
self.__text1.focus() # 鼠标光标定位在文本输入框text1
elif st == 2:
self.__Form1.withdraw()
self.__Form3.withdraw()
self.__Form4.withdraw()
self.__Form5.withdraw()
self.__Center(self.__Form2)
self.__Form2.deiconify()
elif st == 3:
self.__Clear1()
self.__Form1.withdraw()
self.__Form2.withdraw()
self.__Form4.withdraw()
self.__Form5.withdraw()
self.__Center(self.__Form3)
self.__Form3.deiconify()
self.__text11.focus()
elif st == 4:
self.__Form1.withdraw()
self.__Form2.withdraw()
self.__Form3.withdraw()
self.__Form5.withdraw()
self.__Center(self.__Form4)
self.__Form4.deiconify()
self.__text32.focus()
elif st == 5:
self.__Form1.withdraw()
self.__Form2.withdraw()
self.__Form3.withdraw()
self.__Form4.withdraw()
self.__Center(self.__Form5)
self.__Form5.deiconify()
self.__text41.focus()
def __reForm1(self): # 返回登录界面Form1
self.__messbox = tkinter.messagebox.askyesno("提示", "是否返回登录界面?")
if self.__messbox == YES:
self.__Form(1)
# 加盐加密操作
def __Encrypt(self, SaltPwd):
self.__obj = hashlib.md5(SaltPwd.encode("utf-8"))
self.__obj.update(SaltPwd.encode("utf-8"))
return self.__obj.hexdigest()
# 界面1:登录功能
def __Login(self):
self.__user = self.__text1.get().strip()
self.__pwd = self.__text2.get().strip()
self.__CipherText = self.__Encrypt(self.__user + self.__pwd) # 对账号和密码进行加盐加密后的密文
if self.__user == "":
tkinter.messagebox.showinfo("提示", "用户名不得为空!")
self.__text1.focus()
elif self.__pwd == "":
tkinter.messagebox.showinfo("提示", "密码不得为空!")
self.__text2.focus()
elif self.__user + self.__CipherText in self.__List:
tkinter.messagebox.showinfo("提示", "登录成功!")
self.__label22["text"] = self.__user
self.__Clear1()
self.__Form(2)
elif self.__user in self.__Dic.keys() and self.__user + self.__CipherText not in self.__List:
tkinter.messagebox.showinfo("提示", "账号密码错误!")
self.__text2.delete(0, tkinter.END)
self.__text2.focus()
elif self.__user not in self.__Dic.keys():
self.__result = tkinter.messagebox.askyesno("提示", "账号不存在,是否选择注册一个新账号?")
if self.__result == YES:
self.__Clear1()
self.__Form(3)
else:
self.__Clear1()
self.__text1.focus()
# 界面3:注册功能
def __Register(self):
self.__newuser = self.__text11.get().strip()
self.__newpwd = self.__text12.get().strip()
self.__renewpwd = self.__text13.get().strip()
self.__CipherText = self.__Encrypt(self.__newuser + self.__newpwd) # 对账号和密码进行加盐加密后的密文
if self.__newuser == "":
tkinter.messagebox.showinfo("提示", "注册账号不得为空!")
self.__text11.focus()
elif self.__newpwd == "":
tkinter.messagebox.showinfo("提示", "注册账号密码不得为空!")
self.__text12.focus()
elif self.__newpwd != self.__renewpwd:
tkinter.messagebox.showinfo("提示", "两次密码不一致,请重新输入密码!")
self.__text13.delete(0, tkinter.END)
self.__text13.focus()
elif self.__newuser in self.__Dic.keys():
tkinter.messagebox.askyesno("提示", "该账号已注册!请重新输入注册账号!")
self.__Clear3()
else:
tkinter.messagebox.showinfo("提示", "新账号注册成功!")
self.__List.append(self.__newuser + self.__CipherText)
self.__Dic[self.__newuser] = self.__CipherText
self.__Clear3()
self.__Form(1)
# print("注册成功后的账号+密文:", self.__List)
# print("注册成功后的账号和密文:", self.__Dic)
# 界面4:修改密码
def __Change(self):
self.__user = self.__label22["text"]
self.__olduser = self.__text32.get().strip()
self.__newpwd = self.__text33.get().strip()
self.__CipherText = self.__Encrypt(self.__user + self.__olduser) # 对账号和旧密码进行加盐加密后的密文
if self.__user == "":
tkinter.messagebox.showinfo("提示", "账号输入不得为空!")
self.__text32.focus()
elif self.__olduser == "":
tkinter.messagebox.showinfo("提示", "密码不得为空!")
self.__text32.focus()
elif self.__newpwd == "":
tkinter.messagebox.showinfo("提示", "请输入新密码!")
self.__text33.focus()
elif self.__olduser == self.__newpwd:
tkinter.messagebox.showinfo("提示", "请重新输入新密码!")
self.__text33.delete(0, tkinter.END)
self.__text33.focus()
elif self.__user not in self.__Dic.keys():
tkinter.messagebox.showinfo("提示", "账号不存在!")
self.__Clear4()
elif self.__user in self.__Dic.keys() and (self.__user + self.__CipherText) not in self.__List:
tkinter.messagebox.showinfo("提示", "账号密码错误!")
self.__text32.delete(0, tkinter.END)
self.__text33.delete(0, tkinter.END)
elif self.__user + self.__CipherText in self.__List:
tkinter.messagebox.showinfo("提示", "账号密码修改成功!")
self.__List.remove(self.__user + self.__CipherText)
self.__CipherText2 = self.__Encrypt(self.__user + self.__newpwd) # 对账号和新密码进行加盐加密
self.__List.append(self.__user + self.__CipherText2)
self.__Dic[self.__user] = self.__CipherText2
self.__Form(1)
# print("修改后的账号+密文:", self.__List)
# print("修改后的账号和密文:", self.__Dic)
# 界面5:账号注销
def __Cancel(self):
self.__user = self.__label22["text"]
self.__pwd = self.__text41.get().strip()
self.__CipherText = self.__Encrypt(self.__user + self.__pwd) # 对账号和密码进行加盐加密后的密文
if self.__text41.get() == "":
tkinter.messagebox.showinfo("提示", "账号密码不得为空!")
self.__text41.focus()
elif self.__user + self.__CipherText in self.__List:
tkinter.messagebox.showinfo("提示", "账号注销成功!")
self.__List.remove(self.__user + self.__CipherText)
self.__Dic.pop(self.__user)
self.__Clear5()
self.__Form(1)
else:
tkinter.messagebox.showinfo("提示", "账号密码错误!")
self.__Clear5()
# 主程序代码
def Main(self):
self.__Form1 = tkinter.Tk() # 创建一个窗体Form1
self.__Form2 = tkinter.Tk() # 创建一个窗体Form2
self.__Form3 = tkinter.Tk() # 创建一个窗体Form3
self.__Form4 = tkinter.Tk() # 创建一个窗体Form4
self.__Form5 = tkinter.Tk() # 创建一个窗体Form5
self.__Form1.title('登录界面')
self.__Form2.title("主界面")
self.__Form3.title("注册界面")
self.__Form4.title("修改密码")
self.__Form5.title("账号注销")
self.__Center(self.__Form1) # 窗体Form1居中显示
self.__Center(self.__Form2) # 窗体Form2居中显示
self.__Center(self.__Form3) # 窗体Form3居中显示
self.__Center(self.__Form4) # 窗体Form4居中显示
self.__Center(self.__Form5) # 窗体Form5居中显示
# 一次性创建5个窗体后,需要用withdraw()来隐藏其他窗口,只显示一个登录窗口Form1
self.__Form2.withdraw() # 隐藏窗体Form2
self.__Form3.withdraw() # 隐藏窗体Form3
self.__Form4.withdraw() # 隐藏窗体Form4
self.__Form5.withdraw() # 隐藏窗体Form5
# 登录界面Form1代码控件
self.__label1 = tkinter.Label(self.__Form1, text="账号", bg="#C0C0C0", font=('微软雅黑', 18), bd=5, relief='flat', justify="center", )
self.__label1.place(x=self.__width / 13, y=self.__height / 16)
self.__label2 = tkinter.Label(self.__Form1, text="密码", bg="#C0C0C0", font=('微软雅黑', 18), bd=5, relief='flat', justify="center")
self.__label2.place(x=self.__width / 13, y=self.__height / 7.4)
self.__text1 = tkinter.Entry(self.__Form1, font=("微软雅黑", 17), relief="flat", width=int(self.__width / 90), borderwidth=5, justify="center")
self.__text1.place(x=self.__width / 7.5, y=self.__height / 16)
self.__text2 = tkinter.Entry(self.__Form1, font=("微软雅黑", 17), show="*", relief="flat", width=int(self.__width / 90), borderwidth=5, justify="center")
self.__text2.place(x=self.__width / 7.5, y=self.__height / 7.4)
self.__text1.focus() # 鼠标光标定位在文本输入框text1上面
self.__button1 = tkinter.Button(self.__Form1, text="登录", bg="#C0C0C0", activebackground="#ffffff", relief='solid', font=('黑体', 17), padx=15, pady=5, command=self.__Login, )
self.__button1.place(x=self.__width / 7.5, y=self.__height / 4.3)
self.__button2 = tkinter.Button(self.__Form1, text="重置", bg="#C0C0C0", activebackground="#ffffff", relief='solid', font=('黑体', 17), padx=15, pady=5, command=self.__Clear1, )
self.__button2.place(x=self.__width / 3.5, y=self.__height / 4.3)
self.__button3 = tkinter.Button(self.__Form1, text="注册", bg="#C0C0C0", activebackground="#ffffff", relief='solid', font=('黑体', 17), padx=15, pady=5, command=lambda: self.__Form(3), )
self.__button3.place(x=self.__width / 7.5, y=self.__height / 3)
self.__button4 = tkinter.Button(self.__Form1, text="退出", bg="#C0C0C0", activebackground="#ffffff", relief='solid', font=('黑体', 17), padx=15, pady=5, command=self.__Exit)
self.__button4.place(x=self.__width / 3.5, y=self.__height / 3)
# 注册界面Form3代码控件
self.__label11 = tkinter.Label(self.__Form3, text="注册账号", bg="#C0C0C0", font=('微软雅黑', 18), bd=5, relief='flat', justify="center")
self.__label11.place(x=self.__width / 18, y=self.__height / 16)
self.__label12 = tkinter.Label(self.__Form3, text="输入密码", bg="#C0C0C0", font=('微软雅黑', 18), bd=5, relief='flat', justify="center")
self.__label12.place(x=self.__width / 18, y=self.__height / 8)
self.__label13 = tkinter.Label(self.__Form3, text="确认密码", bg="#C0C0C0", font=('微软雅黑', 18), bd=5, relief='flat', justify="center")
self.__label13.place(x=self.__width / 18, y=self.__height / 5.3)
self.__text11 = tkinter.Entry(self.__Form3, font=("微软雅黑", 17), relief="flat", borderwidth=5, width=int(self.__width / 90), justify="center")
self.__text11.place(x=self.__width / 6.7, y=self.__height / 15.5)
self.__text12 = tkinter.Entry(self.__Form3, font=("微软雅黑", 17), relief="flat", show="*", borderwidth=5, width=int(self.__width / 90), justify="center")
self.__text12.place(x=self.__width / 6.7, y=self.__height / 7.7)
self.__text13 = tkinter.Entry(self.__Form3, font=("微软雅黑", 17), relief="flat", show='*', borderwidth=5, width=int(self.__width / 90), justify="center")
self.__text13.place(x=self.__width / 6.7, y=self.__height / 5.2)
self.__button11 = tkinter.Button(self.__Form3, text="确定", bg="#C0C0C0", activebackground="#ffffff", relief='solid', font=('黑体', 17), padx=15, pady=5, command=self.__Register)
self.__button11.place(x=self.__width / 10, y=self.__height / 3.5)
self.__button12 = tkinter.Button(self.__Form3, text="重置", bg="#C0C0C0", activebackground="#ffffff", relief='solid', font=('黑体', 17), padx=15, pady=5, command=self.__Clear3)
self.__button12.place(x=self.__width / 4.6, y=self.__height / 3.5)
self.__button13 = tkinter.Button(self.__Form3, text="取消", bg="#C0C0C0", activebackground="#ffffff", relief='solid', font=('黑体', 17), padx=15, pady=5, command=lambda: self.__Form(1))
self.__button13.place(x=self.__width / 3, y=self.__height / 3.5)
# 主界面Form2代码控件
self.__button21 = tkinter.Button(self.__Form2, text="退出", bg="#C0C0C0", activebackground="#ffffff", relief='solid', font=('黑体', 17), padx=15, pady=5, command=self.__Exit)
self.__button21.place(x=self.__width / 3.5, y=self.__height / 2.7)
self.__button22 = tkinter.Button(self.__Form2, text="修改密码", bg="#C0C0C0", activebackground="#ffffff", relief='solid', font=("黑体", 17), padx=15, pady=5, command=lambda: self.__Form(4))
self.__button22.place(x=self.__width / 10, y=self.__height / 3.8)
self.__button23 = tkinter.Button(self.__Form2, text="账号注销", bg="#C0C0C0", activebackground="#ffffff", relief='solid', font=("黑体", 17), padx=15, pady=5, command=lambda: self.__Form(5), )
self.__button23.place(x=self.__width / 3.7, y=self.__height / 3.8)
self.__button24 = tkinter.Button(self.__Form2, text="返回", bg="#C0C0C0", activebackground="#ffffff", relief='solid', font=("黑体", 17), padx=15, pady=5, command=self.__reForm1, )
self.__button24.place(x=self.__width / 8.8, y=self.__height / 2.7)
self.__label21 = tkinter.Label(self.__Form2, text="账号:", font=("微软雅黑", 18), padx=10, pady=10, bg="#C0C0C0", relief="flat", justify="center", )
self.__label21.place(x=self.__width / 22, y=self.__height / 18)
self.__label22 = tkinter.Label(self.__Form2, text=self.__text1.get(), font=("微软雅黑", 18), padx=10, pady=10, bg="#C0C0C0", relief="flat", justify="left", )
self.__label22.place(x=self.__width / 10, y=self.__height / 18)
# 修改密码界面Form4代码控件
self.__label32 = tkinter.Label(self.__Form4, text="旧密码", bg="#C0C0C0", font=('微软雅黑', 18), bd=5, relief='flat', justify="center")
self.__label32.place(x=self.__width / 16, y=self.__height / 12)
self.__label33 = tkinter.Label(self.__Form4, text="新密码", bg="#C0C0C0", font=('微软雅黑', 18), bd=5, relief='flat', justify="center")
self.__label33.place(x=self.__width / 16, y=self.__height / 7)
self.__text32 = tkinter.Entry(self.__Form4, font=("微软雅黑", 17), relief="flat", borderwidth=5, width=int(self.__width / 90), justify="center")
self.__text32.place(x=self.__width / 7, y=self.__height / 11.5)
self.__text33 = tkinter.Entry(self.__Form4, font=("微软雅黑", 17), show='*', relief="flat", borderwidth=5, width=int(self.__width / 90), justify="center")
self.__text33.place(x=self.__width / 7, y=self.__height / 6.8)
self.__button31 = tkinter.Button(self.__Form4, text="确定", bg="#C0C0C0", activebackground="#ffffff", relief='solid', font=('黑体', 17), padx=15, pady=5, command=self.__Change)
self.__button31.place(x=self.__width / 9.5, y=self.__height / 3.8)
self.__button32 = tkinter.Button(self.__Form4, text="重置", bg="#C0C0C0", activebackground="#ffffff", relief='solid', font=('黑体', 17), padx=15, pady=5, command=self.__Clear4)
self.__button32.place(x=self.__width / 4.5, y=self.__height / 3.8)
self.__button33 = tkinter.Button(self.__Form4, text="取消", bg="#C0C0C0", activebackground="#ffffff", relief='solid', font=('黑体', 17), padx=15, pady=5, command=lambda: self.__Form(2))
self.__button33.place(x=self.__width / 3, y=self.__height / 3.8)
# 账号注销界面Form5代码控件
self.__label41 = tkinter.Label(self.__Form5, text="账号密码", bg="#C0C0C0", font=('微软雅黑', 18), bd=5, relief='flat', justify="center")
self.__label41.place(x=self.__width / 18, y=self.__height / 10)
self.__text41 = tkinter.Entry(self.__Form5, font=("微软雅黑", 17), relief="flat", borderwidth=5, width=int(self.__width / 95), justify="center")
self.__text41.place(x=self.__width / 6.8, y=self.__height / 9.6)
self.__button41 = tkinter.Button(self.__Form5, text="注销", bg="#C0C0C0", activebackground="#ffffff", relief='solid', font=('黑体', 17), padx=15, pady=5, command=self.__Cancel, )
self.__button41.place(x=self.__width / 10, y=self.__height / 4.5)
self.__button42 = tkinter.Button(self.__Form5, text="重置", bg="#C0C0C0", activebackground="#ffffff", relief='solid', font=('黑体', 17), padx=15, pady=5, command=self.__Clear5, )
self.__button42.place(x=self.__width / 4.7, y=self.__height / 4.5)
self.__button43 = tkinter.Button(self.__Form5, text="取消", bg="#C0C0C0", activebackground="#ffffff", relief='solid', font=('黑体', 17), padx=15, pady=5, command=lambda: self.__Form(2), )
self.__button43.place(x=self.__width / 3, y=self.__height / 4.5)
self.__Form5.mainloop()
self.__Form4.mainloop()
self.__Form3.mainloop()
self.__Form2.mainloop()
self.__Form1.mainloop()
if __name__ == '__main__':
st = FForm()
st.Main()
这里还有一个是非窗体程序代码,也是通过列表和字典来实现的,
封装后的代码如下:
import sys
class ARR:
def __init__(self):
__List = [] # 新建一个列表,设为私有变量,或者__List=list()
__Dic = dict() # 新建一个字典,设为私有变量
self.__List = __List
self.__Dic = __Dic
def __Login(self):
print("\n-------------- 1.账号登录 -------------")
self.__user = input("请输入账号:").strip()
self.__pwd = input("请输入密码:").strip()
while True:
if self.__user == "":
self.__user = input("账号不得为空!\n请输入账号:").strip()
elif self.__pwd == "":
self.__pwd = input("密码不得为空!\n请输入密码:").strip()
elif self.__user not in self.__Dic.keys():
self.__num = input("账号不存在!是否选择注册一个新账号?\n1、是;2、否\n").strip()
if self.__num == "1":
self.__Register()
elif self.__num == "2":
print()
self.Main()
else:
num = input("请重新输入选择:").strip()
self.Main()
elif self.__user + self.__pwd in self.__List:
print("登录成功!\n")
self.Main()
else:
print("账号密码错误!")
self.__Login()
def __Register(self):
print("\n-------------- 2.账号注册 -------------")
self.__user = input("请输入账号:").strip()
self.__pwd = input("请输入密码:").strip()
self.__repwd = input("请确认密码:").strip()
while True:
if self.__user == "":
self.__user = input("账号不得为空!\n请输入账号:").strip()
elif self.__pwd == "":
self.__pwd = input("密码不得为空!\n请输入密码:").strip()
elif self.__repwd == "" or self.__repwd != self.__pwd:
self.__repwd = input("请重新确认密码:").strip()
elif self.__user in self.__Dic.keys():
print("该账号已注册!")
self.__Register()
else:
print("账号注册成功!\n")
self.__List.append(self.__user + self.__pwd)
self.__Dic[self.__user] = self.__pwd
self.Main()
def __Change(self):
print("\n-------------- 3.修改密码 -------------")
self.__user = input("请输入账号:").strip()
self.__oldpwd = input("请输入旧密码:").strip()
self.__newpwd = input("请输入新密码:").strip()
while True:
if self.__user == "":
self.__user = input("账号不得为空!\n请输入账号:").strip()
elif self.__oldpwd == "":
self.__oldpwd = input("密码不得为空!\n请输入密码:").strip()
elif self.__newpwd == "":
self.__newpwd = input("新密码不得为空!\n请输入新密码:").strip()
elif self.__oldpwd == self.__newpwd:
self.__newpwd = input("请重新输入新密码:").strip()
elif self.__user not in self.__Dic.keys():
print("账号不存在!")
self.__Change()
elif self.__user + self.__oldpwd in self.__List:
print("账号密码修改成功!\n")
self.__List.remove(self.__user + self.__oldpwd)
self.__List.append(self.__user + self.__newpwd)
self.__Dic[self.__user] = self.__newpwd
self.Main()
else:
print("账号密码错误!")
self.__Change()
def __Cancel(self):
print("\n-------------- 4.账号注销 -------------")
self.__user = input("请输入账号:").strip()
self.__pwd = input("请输入密码:").strip()
while True:
if self.__user == "":
self.__user = input("账号不得为空!\n请输入账号:").strip()
elif self.__pwd == "":
self.__pwd = input("密码不得为空!\n请输入密码:").strip()
elif self.__user not in self.__Dic.keys():
print("账号不存在!")
self.Main()
elif self.__user + self.__pwd in self.__List:
print("账号注销成功!\n")
self.__List.remove(self.__user + self.__pwd)
self.__Dic.pop(self.__user)
self.Main()
else:
print("账号密码错误!")
self.__Change()
def Main(self):
print("1、账号登录;2、账号注册;3、修改密码;4、账号注销;0、退出程序")
self.__num = input("请输入选择:").strip()
while True:
if self.__num == "1":
self.__Login()
elif self.__num == "2":
self.__Register()
elif self.__num == "3":
self.__Change()
elif self.__num == "4":
self.__Cancel()
elif self.__num == "0":
print("程序已退出!")
sys.exit(0)
else:
self.__num = input("请重新输入选择:")
if __name__ == '__main__':
st = ARR()
st.Main()
以上就是主要代码,这次是通过新建列表和字典来存储和验证账号和密码,感兴趣的小伙伴可以来看看,可能还有一些不足之处,也欢迎大家指出。之前我是通过连接SQL Server数据库来实现:Python Tkinter窗体程序连接SQL Server数据库实现账号登录、账号注册等功能。