Python简易用户登录以及一些小工具
本帖最后由 taoyuanhang 于 2023-7-31 14:35 编辑这已经是我写的第八个版本的用户登录了
8.0相比7.0有更高的安全性,有全新外观
用户登录8.0源码:
其中,b()函数是之前单用户登录留下来的函数,不舍得删,留到了多用户就没有用了,你们测试时可以删掉。
有bug可以跟我说,只要是发现bug的,并且在我可以改的能力范围之内的,我都会非常细心的修改,并且会有一个特别版的用户登录,敬请期待!
顺便问大佬们一个问题,easygui如果开了多个窗口的话,就会冲突,甚至造成卡死等情况,有没有办法解决?具体是怎么样,你可以连续点击“我是管理员”和“更多”按钮,就会触发这个Bug了。
还有,问题发在这里是不是有点不太合适?可以去这个帖子回答(目前审核可能未通过)->https://www.52pojie.cn/thread-1815498-1-1.html
global gly_login
gly_login = 0
def main():
def isgly():
try:
with open("gly.txt") as f:
if f.read() == "enabled":
return True
else:
return False
except:
return False
import os
import tkinter as tk
import tkinter.messagebox
from Crypto.Cipher import AES
import base64
import sys
import ttkbootstrap as ttk
import hashlib
try:
import easygui
except:
tk.messagebox.showerror("错误", "无法导入easygui模块!")
sys.exit()
import subprocess
BLOCK_SIZE = 16# Bytes
pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * chr(
BLOCK_SIZE - len(s) % BLOCK_SIZE
)
unpad = lambda s: s[: -ord(s)]
def aesEncrypt(key, data):
key = key.encode("utf8")
data = pad(data)
cipher = AES.new(key, AES.MODE_ECB)
result = cipher.encrypt(data.encode())
encodestrs = base64.b64encode(result)
enctext = encodestrs.decode("utf8")
return enctext
def aesDecrypt(key, data):
"""
:param key: 密钥
:param data: 加密后的数据(密文)
:return:明文
"""
key = key.encode("utf8")
data = base64.b64decode(data)
cipher = AES.new(key, AES.MODE_ECB)
text_decrypted = unpad(cipher.decrypt(data))
text_decrypted = text_decrypted.decode("utf8")
return text_decrypted
def saveinfo():
with open("nameandpwd.txt", "w") as f:
f.write(aesEncrypt(key="taoyuanhang66666", data=str(nameandpwd)))
def readinfo():
with open("nameandpwd.txt") as f:
nameandpwd = eval(aesDecrypt(key="taoyuanhang66666", data=f.read()))
win = ttk.Window(alpha=0.8, themename="darkly")
win.attributes("-topmost", 1)
win.title("用户登录 8.0")
win.geometry("250x140")
win.resizable(width=False, height=False)
try:
win.iconbitmap("tubiao.ico")
except:
pass
try:
with open("nameandpwd.txt") as f:
nameandpwd = eval(aesDecrypt(key="taoyuanhang66666", data=f.read()))
except:
with open("nameandpwd.txt", "w") as f:
f.write(aesEncrypt(key="taoyuanhang66666", data="{'admin': 'python@16'}"))
nameandpwd = {"admin": "python@16"}
var_Name = ttk.StringVar()
var_Name.set("")
var_Pwd = ttk.StringVar()
var_Pwd.set("")
def about(a):
if a == "1":
tk.messagebox.showinfo(
"About",
"""用户登录(管理员) by 陶远航
版本:8.0 公测版
程序及源代码仅供学习交流,
未经作者允许不可用于商业用途!
""",
)
elif a == "2":
tk.messagebox.showinfo(
"About",
"""你正在管理员模式查看关于
用户登录(管理员) by 陶远航
版本:8.0 公测版.
程序及源代码仅供学习交流,
未经作者允许不可用于商业用途!
""",
)
def feedback():
import smtplib
import re
from os import environ
from os.path import exists
from platform import system, node
from time import strftime
from email.mime.text import MIMEText
from email.utils import formataddr
from random import randint
from easygui import enterbox
title = "用户登录 8.0"
my_sender = "advance_software@126.com"
my_pass = "QFAQPLFQZRZBMVWQ"
dt = strftime("%Y-%m-%d %H:%M:%S")
my_user = "taoyuanhang66@outlook.com"
username = environ["USERNAME"]
system = system()
computer = node()
number = enterbox("请输入反馈内容:")
err = Exception
def mail():
global err
ret = True
try:
msg = MIMEText(number, "plain", "utf-8")
msg["From"] = formataddr()
msg["To"] = formataddr(["", my_user])
msg["Subject"] = "用户使用反馈"
server = smtplib.SMTP_SSL("smtp.126.com", 465)
server.login(my_sender, my_pass)
server.sendmail(
my_sender,
[
my_user,
],
msg.as_string(),
)
server.quit()
except Exception as e:
ret = False
err = str(e)
return ret
def checkmail(email):
reg = "\w+[@]+(\.+)+"
result = re.findall(reg, email)
if result:
ret = mail()
if ret:
tk.messagebox.showinfo("反馈", "发送成功!")
else:
tk.messagebox.showerror("反馈", "邮件发送失败!")
else:
tk.messagebox.showerror("用户登录", "您的输入不合法,请重新输入!")
if __name__ == "__main__":
checkmail(my_user)
def tools(name, is_gly=False):
import easygui
while True:
list = [
"打开记事本",
"简易计算器",
"修改密码",
"重启主窗口",
"摄氏度转华氏度",
"华氏度转摄氏度",
"关于",
"意见反馈",
"用户信息",
"退出",
]
if is_gly == True:
list = [
"打开记事本",
"简易计算器",
"重启主窗口",
"摄氏度转华氏度",
"华氏度转摄氏度",
"关于",
"意见反馈",
"用户信息",
"退出",
]
choise = easygui.choicebox(msg="选择小工具", title="选择小工具", choices=list)
if choise == "打开记事本":
subprocess.Popen(["notepad.exe"])
elif choise == "简易计算器":
try:
shizi = easygui.enterbox("输入要计算的式子:")
tk.messagebox.showinfo("简易计算器", eval(shizi))
except:
tk.messagebox.showerror("简易计算器", "输入错误,请重新输入")
elif choise == "退出":
if tk.messagebox.askyesno("小工具", "确定退出吗?") == True:
break
else:
pass
elif choise == "意见反馈":
feedback()
elif choise == "摄氏度转华氏度":
try:
ssd = float(easygui.enterbox("请输入摄氏度:"))
hsd = format(ssd * 1.8 + 32, ".3f")
tk.messagebox.showinfo("摄氏度转华氏度", hsd)
except:
tk.messagebox.showinfo("摄氏度转华氏度", "请输入正确的值")
elif choise == "华氏度转摄氏度":
try:
hsd = float(easygui.enterbox("请输入华氏度:"))
ssd = format((hsd - 32) / 1.8, ".3f")
tk.messagebox.showinfo("华氏度转摄氏度", ssd)
except:
tk.messagebox.showinfo("摄氏度转华氏度", "请输入正确的值")
elif choise == "小游戏":
try:
os.system("plane_main.exe")
except:
tk.messagebox.showerror("错误", "没有小游戏")
elif choise == "关于":
if is_gly == True:
about("2")
else:
about("1")
elif choise == "修改密码":
try:
pwd = nameandpwd
except:
tk.messagebox.showerror("", "未找到此用户!")
if easygui.enterbox("请输入旧密码") == pwd:
pwd_new = easygui.enterbox("请输入新密码")
nameandpwd = pwd_new
with open("nameandpwd.txt", "w") as f:
f.write(
aesEncrypt(key="taoyuanhang66666", data=str(nameandpwd))
)
tk.messagebox.showinfo("", "修改成功!")
else:
tk.messagebox.showerror("", "密码错误!")
elif choise == "用户信息":
tk.messagebox.showinfo("用户信息", ("用户名:", name))
elif choise == "重启主窗口":
main()
break
else:
if tk.messagebox.askyesno("小工具", "你确定要退出吗") == True:
break
else:
pass
def b():
try:
with open("a.txt") as f:
temp = f.read()
if temp == "ok":
with open("a.txt", "w") as f:
f.write("4")
tk.messagebox.showerror(title="用户登录", message="还有4次机会,错误次数过多会导致账户永久锁定!")
else:
try:
temp = int(temp)
except:
tk.messagebox.showerror(title="用户登录", message="账号异常!")
if temp == 1:
tk.messagebox.showerror(
title="用户登录", message="错误次数过多,账户已永久锁定,请重新注册!"
)
else:
temp = temp - 1
with open("a.txt", "w") as f:
f.write(str(temp))
tk.messagebox.showerror(
title="用户登录", message=("还有", str(temp), "次机会,错误次数过多会导致账户永久锁定!")
)
except:
with open("a.txt", "w") as f:
f.write("4")
tk.messagebox.showerror("用户登录", "还有4次机会,错误次数过多会导致账户永久锁定!")
def login():
try:
win.attributes("-topmost", 0)
except:
pass
try:
with open("nameandpwd.txt") as f:
nameandpwd = eval(aesDecrypt(key="taoyuanhang66666", data=f.read()))
except:
with open("nameandpwd.txt", "w") as f:
f.write(
aesEncrypt(key="taoyuanhang66666", data="{'admin': 'python@16'}")
)
nameandpwd = {"admin": "python@16"}
name = var_Name.get()
pwd = var_Pwd.get()
try:
if nameandpwd == pwd:
if name.upper() == "SYSTEM":
tk.messagebox.showerror("错误", '不能以"SYSTEM"为用户名')
else:
tk.messagebox.showinfo("用户登录", "登录成功!")
with open("a.txt", "w") as f:
f.write("ok")
win.quit()
win.destroy()
tools(name)
else:
tk.messagebox.showerror("用户登录", "密码错误!")
except:
tk.messagebox.showerror("用户登录", "没有此用户!")
def zhuce():
try:
win.attributes("-topmost", 0)
except:
pass
try:
win.attributes("-topmost", 0)
except:
pass
try:
with open("nameandpwd.txt") as f:
nameandpwd = eval(aesDecrypt(key="taoyuanhang66666", data=f.read()))
except:
with open("nameandpwd.txt", "w") as f:
f.write(
aesEncrypt(key="taoyuanhang66666", data="{'admin': 'python@16'}")
)
nameandpwd = {"admin": "python@16"}
name = var_Name.get()
pwd = var_Pwd.get()
try:
nameandpwd
tk.messagebox.showerror("", "该用户名已被注册,请换一个吧!")
except:
if name == "" or pwd == "":
tk.messagebox.showerror(title="警告", message="用户名或密码不能为空")
if name.upper() == "SYSTEM":
tk.messagebox.showerror(title="警告", message='不能以"SYSTEM"作为用户名')
else:
if len(pwd) <= 5:
tk.messagebox.showinfo(title="警告", message="密码太短了,长度不能低于6个字符!")
elif pwd.count(pwd) == len(pwd):
tk.messagebox.showinfo(title="警告", message="密码不安全,换个不容易被猜到的密码吧!")
elif pwd in [
"A123456",
"a123456",
"a1234567",
"A1234567",
"Aa1234567",
"password",
"Password",
"password1",
"a123456",
"qwerty",
"password01",
"picture1",
]:
tk.messagebox.showinfo(title="警告", message="密码不安全,换个不容易被猜到的密码吧!")
else:
pwd2 = easygui.passwordbox("请再次输入密码:")
if pwd == pwd2:
nameandpwd = pwd
with open("nameandpwd.txt", "w") as f:
f.write(
aesEncrypt(key="taoyuanhang66666", data=str(nameandpwd))
)
tk.messagebox.showinfo("注册", "注册成功!")
else:
tk.messagebox.showerror("注册", "密码不一致!")
def _quit():
try:
win.attributes("-topmost", 0)
except:
pass
import smtplib
import re
from os import environ
from os.path import exists
from platform import system, node
from time import strftime
from email.mime.text import MIMEText
from email.utils import formataddr
from random import randint
from easygui import enterbox
title = "用户登录 8.0"
my_sender = "advance_software@126.com"
my_pass = "QFAQPLFQZRZBMVWQ"
dt = strftime("%Y-%m-%d %H:%M:%S")
my_user = enterbox("请输入邮箱:")
username = environ["USERNAME"]
system = system()
computer = node()
number = randint(100000, 999999)
number = str(number)
err = Exception
def mail():
global err
ret = True
try:
msg = MIMEText(number, "plain", "utf-8")
msg["From"] = formataddr(["陶远航", my_sender])
msg["To"] = formataddr(["", my_user])
msg["Subject"] = "用户登录的验证码"
server = smtplib.SMTP_SSL("smtp.126.com", 465)
server.login(my_sender, my_pass)
server.sendmail(
my_sender,
[
my_user,
],
msg.as_string(),
)
server.quit()
except Exception as e:
ret = False
err = str(e)
return ret
def checkmail(email):
reg = "\w+[@]+(\.+)+"
result = re.findall(reg, email)
if result:
ret = mail()
if ret:
num = enterbox("发送成功!请耐心等待并输入您的验证码:", title)
if num == str(number):
tk.messagebox.showinfo("用户登录", "登录成功!")
win.quit()
win.destroy()
tools()
else:
tk.messagebox.showinfo("用户登录", "登录失败!")
else:
tk.messagebox.showerror("用户登录", "邮件发送失败!")
else:
tk.messagebox.showerror("用户登录", "您的输入不合法,请重新输入!")
if __name__ == "__main__":
checkmail(my_user)
def gly():
try:
with open("nameandpwd.txt") as f:
nameandpwd = eval(aesDecrypt(key="taoyuanhang66666", data=f.read()))
except:
with open("nameandpwd.txt", "w") as f:
f.write(
aesEncrypt(key="taoyuanhang66666", data="{'admin': 'python@16'}")
)
nameandpwd = {"admin": "python@16"}
if isgly():
listgly = [
"退出",
"注销账号",
"查看所有用户",
"添加账户",
"查看密码",
"强制登录",
"带有用户名的强制登录(高级)",
"关于",
]
else:
pass
while True:
if isgly():
choice = easygui.choicebox(msg="我是管理员", title="我是管理员", choices=listgly)
else:
tk.messagebox.showerror("", "你没有管理员权限,请退出!")
break
if choice == "注销账号":
readinfo()
if isgly():
pass
else:
tk.messagebox.showerror("", "你没有管理员权限,请退出!")
break
name = easygui.enterbox("请输入用户名", "请输入用户名")
try:
nameandpwd.pop(name)
with open("nameandpwd.txt", "w") as f:
f.write(
aesEncrypt(key="taoyuanhang66666", data=str(nameandpwd))
)
tk.messagebox.showinfo("注销", "注销成功!")
except:
tk.messagebox.showerror("错误", "没有此用户!")
elif choice == "查看所有用户":
readinfo()
if isgly():
pass
else:
tk.messagebox.showerror("", "你没有管理员权限,请退出!")
break
nms = list(nameandpwd.keys())
tk.messagebox.showinfo("用户", nms)
elif choice == "添加账户":
readinfo()
if isgly():
pass
else:
tk.messagebox.showerror("", "你没有管理员权限,请退出!")
break
name = easygui.enterbox("请输入用户名")
nameandpwd = easygui.enterbox("请输入密码")
with open("nameandpwd.txt", "w") as f:
f.write(aesEncrypt(key="taoyuanhang66666", data=str(nameandpwd)))
tk.messagebox.showinfo("", "添加成功!")
elif choice == "查看密码":
readinfo()
if isgly():
pass
else:
tk.messagebox.showerror("", "你没有管理员权限,请退出!")
break
try:
tk.messagebox.showinfo(
"密码", nameandpwd
)
except:
tk.messagebox.showerror("警告", "没有此用户")
elif choice == "强制登录":
readinfo()
if isgly():
pass
else:
tk.messagebox.showerror("", "你没有管理员权限,请退出!")
break
try:
win.quit()
win.destroy()
except:
pass
tools("SYSTEM", is_gly=True)
elif choice == "带有用户名的强制登录(高级)":
readinfo()
if isgly():
pass
else:
tk.messagebox.showerror("", "你没有管理员权限,请退出!")
break
try:
win.quit()
win.destroy()
except:
pass
tools(easygui.enterbox("请输入登录的用户名"), is_gly=True)
elif choice == "关于":
if isgly():
pass
else:
tk.messagebox.showerror("", "你没有管理员权限,请退出!")
break
about("2")
else:
if tk.messagebox.askyesno("警告", "你确定要退出吗?"):
break
def winset():
winlist = ["更改标题", "将主窗口设置为可调整大小", "将主窗口设置为不可调整大小", "更改主窗口大小", "退出"]
while True:
choice = easygui.choicebox(msg="窗口设置", title="窗口设置", choices=winlist)
if choice == "更改标题":
win.title(easygui.enterbox("输入窗口标题", "输入窗口标题"))
elif choice == "将主窗口设置为可调整大小":
win.resizable(width=True, height=True)
elif choice == "将主窗口设置为不可调整大小":
win.resizable(width=False, height=False)
elif choice == "更改主窗口大小":
a = easygui.enterbox("输入窗口大小,如:250x150")
try:
win.geometry(a)
except:
tk.messagebox.showinfo("", "设置错误。注意:250x150中间的“x”要用乘号")
else:
break
def more():
try:
win.attributes("-topmost", 0)
except:
pass
try:
with open("nameandpwd.txt") as f:
nameandpwd = eval(aesDecrypt(key="taoyuanhang66666", data=f.read()))
except:
with open("nameandpwd.txt", "w") as f:
f.write(
aesEncrypt(key="taoyuanhang66666", data="{'admin': 'python@16'}")
)
nameandpwd = {"admin": "python@16"}
list = ["注销账号", "窗口设置", "窗口置顶", "取消窗口置顶", "管理员按钮激活", "取消激活管理员按钮", "退出"]
while True:
choice = easygui.choicebox(msg="更多", title="更多", choices=list)
if choice == "退出":
if tk.messagebox.askyesno("警告", "你确定要退出吗"):
break
elif choice == "注销账号":
readinfo()
name = easygui.enterbox("输入你要注销的用户名:", "注销")
pwd = easygui.passwordbox("输入此用户的密码:", "注销")
try:
pwd2 = nameandpwd
if pwd == pwd2:
if tk.messagebox.askyesno("警告", "你确定要注销吗?"):
nameandpwd.pop(name)
with open("nameandpwd.txt", "w") as f:
f.write(
aesEncrypt(
key="taoyuanhang66666", data=str(nameandpwd)
)
)
tk.messagebox.showinfo("注销", "注销成功!")
else:
tk.messagebox.showerror("警告", "密码错误!")
except:
tk.messagebox.showerror("警告", "没有此用户!")
elif choice == "取消激活管理员按钮":
if tk.messagebox.askyesno("警告", "这将使你失去管理员权限,继续吗?"):
try:
with open("gly.txt") as f:
if f.read() == "enabled":
temp = True
else:
temp = False
except:
temp = False
if temp:
with open("gly.txt", "w") as f:
f.write("disabled")
but_gly = tk.Button(
win, text="我是管理员", command=glylogin, state="disabled"
)
but_gly.place(x=30, y=110, width=90, height=20)
tk.messagebox.showinfo("", "取消激活成功!")
else:
tk.messagebox.showinfo("", "你没有管理员权限")
elif choice == "窗口设置":
if mima():
winset()
elif choice == "管理员按钮激活":
try:
with open("gly.txt") as f:
temp = f.read()
except:
temp = ""
if temp == "enabled":
tk.messagebox.showinfo("", "您已激活,无需再次激活")
elif (
hashlib.md5(
str(easygui.passwordbox("请输入激活密码")).encode()
).hexdigest()
== "31cf31b94356a2a6137641698fdfe304"
):
with open("gly.txt", "w") as f:
f.write("enabled")
but_gly = tk.Button(win, text="我是管理员", command=glylogin)
but_gly.place(x=30, y=110, width=90, height=20)
tk.messagebox.showinfo("", "激活成功")
else:
tk.messagebox.showerror("", "密码错误")
elif choice == "窗口置顶":
win.attributes("-topmost", 1)
tk.messagebox.showinfo("", "置顶成功!")
elif choice == "取消窗口置顶":
win.attributes("-topmost", 0)
tk.messagebox.showinfo("", "取消置顶成功!")
else:
if tk.messagebox.askyesno("警告", "你确定要退出吗"):
break
def mima():
tk.messagebox.showinfo(
"管理员验证",
"""功能内测中或仅限管理员调试使用,
请确定你的管理员身份""",
)
glypwd = easygui.passwordbox("请输入密码:")
if (
hashlib.md5(str(glypwd).encode()).hexdigest()
== "fd2e8045450ce3baca670d5cc1b3cd5f"
):
glypwd = easygui.passwordbox("请输入第二层密码:")
if (
hashlib.md5(str(glypwd).encode()).hexdigest()
== "31cf31b94356a2a6137641698fdfe304"
):
return True
else:
tk.messagebox.showerror("", "密码错误!")
return False
else:
tk.messagebox.showerror("", "密码错误!")
return False
def glylogin():
if isgly():
try:
win.attributes("-topmost", 0)
except:
pass
global gly_login
if gly_login == 1:
tk.messagebox.showinfo("", "已确定你的管理员身份,已自动登录")
gly()
elif isgly():
if mima():
gly_login = 1
gly()
else:
if tk.messagebox.askyesno(
"",
"""管理员按钮未激活,您的使用将会受限.
若要体验全功能,请激活管理员按钮,是否获取管理员密码?""",
):
tk.messagebox.showinfo("获取管理员密码", "有兴趣的朋友可以尝试解密!")
else:
but_gly = tk.Button(win, text="我是管理员", command=glylogin, state="disabled")
but_gly.place(x=30, y=110, width=90, height=20)
tk.messagebox.showerror("", "你没有管理员权限,有兴趣的朋友可以尝试解密!")
labname = tk.Label(win, text="账号:", width=80)
labpwd = tk.Label(win, text="密码:", width=80)
entname = tk.Entry(win, width=100, textvariable=var_Name)
entpwd = tk.Entry(win, show="*", width=100, textvariable=var_Pwd)
but_Ok = tk.Button(win, text="登录", command=login)
but_Cancel = tk.Button(win, text="注册", command=zhuce)
but_quit = tk.Button(win, text="邮箱验证", command=_quit)
if isgly():
but_gly = tk.Button(win, text="我是管理员", command=glylogin)
else:
but_gly = tk.Button(win, text="我是管理员", command=glylogin)
but_more = tk.Button(win, text="更多", command=more)
labname.place(x=20, y=10, width=80, height=20)
labpwd.place(x=20, y=40, width=80, height=20)
entname.place(x=120, y=10, width=80, height=20)
entpwd.place(x=120, y=40, width=80, height=20)
but_Ok.place(x=30, y=80, width=50, height=20)
but_Cancel.place(x=100, y=80, width=50, height=20)
but_quit.place(x=160, y=80, width=70, height=20)
but_gly.place(x=30, y=110, width=90, height=20)
but_more.place(x=140, y=110, width=50, height=20)
def on_closing():
if tkinter.messagebox.askyesno("警告", "你确定要退出吗?"):
win.quit()
win.destroy()
win.protocol("WM_DELETE_WINDOW", on_closing)
win.mainloop()
main()
Pycharm 专业版 python 3.8 报错。 本帖最后由 taoyuanhang 于 2023-7-31 17:38 编辑
huiker231 发表于 2023-7-31 17:09
楼主在哪里学的Python呀,我也想偶尔瞄一眼,希望推荐一下,或说说学习想法、思路
Python视频学习资料:
链接: https://pan.baidu.com/s/1doFhSoZNOGX5kVw8jOBC8Q?pwd=52pj
提取码: 52pj
或者你可以看看小甲鱼在B站的视频,我入门的时候看的就是他的教程 这么多行代码,不简易了吧:lol 程序界面能贴个图吗 xinxiu 发表于 2023-7-31 16:21
这么多行代码,不简易了吧
还有一些小工具,删去的话可能就三百多行了 楼主在哪里学的Python呀,我也想偶尔瞄一眼,希望推荐一下,或说说学习想法、思路 感谢分享,看见python帖子必须回复支持 taoyuanhang 发表于 2023-7-31 17:33
Python视频学习资料:
链接: https://pan.baidu.com/s/1doFhSoZNOGX5kVw8jOBC8Q?pwd=52pj
提取码: 52pj ...
你用Python3.*几啊,电脑操作系统呢? huiker231 发表于 2023-7-31 17:44
你用Python3.*几啊,电脑操作系统呢?
Python3.11,Win11 taoyuanhang 发表于 2023-7-31 17:45
Python3.11,Win11
哦,都3.11了,好快啊,看样子我到时候,得重新配电脑了,我现在的是win7*32的