# *-* coding:utf8 *-*
import tkinter as tk
import tkinter.messagebox
window = tk.Tk() # 实例化Tk
window.title("login") # 设置标题
window.geometry("450x300") # 设置窗口的大小
user_info = [{"name": "admin", "pwd": "1"}, {"name": "user", "pwd": "1"}] # 存储账号信息
# 定义函数
def login():
user_info_name = [i["name"] for i in user_info]
user_info_pwd = [i["pwd"] for i in user_info]
user_name = var_user_name.get()
user_pwd = var_pwd.get()
if user_name in user_info_name:
if user_pwd == user_info_pwd[user_info_name.index(user_name)]:
tk.messagebox.showinfo(title="欢迎回来", message="How are you")
else:
tk.messagebox.showerror(message="pwd error")
else:
answer = tk.messagebox.askquestion(title="warn", message="您还没有账号,是否需要注册?")
if answer:
sign()
def sign():
def store():
new_pwd1 = new_pwd.get()
new_user_name1 = new_user_name.get()
user_info.append({"name": new_user_name1, "pwd": new_pwd1})
tk.messagebox.showinfo(message="注册成功")
window_sign.destroy() # 关闭窗口
def close():
window_sign.destroy()
window_sign = tk.Toplevel(window) # 窗口上的窗口
window_sign.geometry("350x200")
window_sign.title("Sign up window")
# user
tk.Label(window_sign, text="Name:").place(x=60, y=60)
new_user_name = tk.StringVar() # 注意要使用.get()得到值
new_user_name.set("exampe@python.com") # 默认值
tk.Entry(window_sign, textvariable=new_user_name).place(x=90, y=60) # 输入用户名
# pwd
tk.Label(window_sign, text="Pwd:").place(x=60, y=100)
new_pwd = tk.StringVar()
tk.Entry(window_sign, textvariable=new_pwd, show="*").place(x=90, y=100) # 输入密码
# 确认按钮
tk.Button(window_sign, text="确认", command=store).place(x=100, y=140)
# 取消按钮
tk.Button(window_sign, text="取消", command=close).place(x=170, y=140)
if __name__ == '__main__':
tk.Label(window, text="User Name:").place(x=50, y=50)
var_user_name = tk.StringVar() # 注意要使用.get()得到值
var_user_name.set("exampe@python.com") # 默认值
tk.Entry(window, textvariable=var_user_name).place(x=130, y=50) # 输入用户名
tk.Label(window, text="Password:").place(x=50, y=90)
var_pwd = tk.StringVar()
tk.Entry(window, textvariable=var_pwd, show="*").place(x=130, y=90) # 输入密码
# login Button
tk.Button(window, text="Login", command=login).place(x=130, y=130) # 登录按钮
# sign Button
tk.Button(window, text="Sign Up", command=sign).place(x=200, y=130) # 注册按钮
window.mainloop() # 显示窗口
使用tkinter编写界面
于 2022-06-16 16:22:35 首次发布