我玩一个新语言的时候都会拿这个语言写一个登陆窗口,可能是因为登陆窗口简单吧。。。
import tkinter as tk
import pickle
import tkinter.messagebox
# 基础弹窗
window = tk.Tk()
window.title('Welcome')
window.geometry('450x300')
#用户登录界面
tk.Label(window, text='User name: ').place(x=50, y= 150)
tk.Label(window, text='Password: ').place(x=50, y= 190)
var_usr_name = tk.StringVar()
var_usr_name.set('example@qq.com')
entry_usr_name = tk.Entry(window, textvariable=var_usr_name)
entry_usr_name.place(x=160, y=150)
var_usr_pwd = tk.StringVar()
entry_usr_pwd = tk.Entry(window, textvariable=var_usr_pwd, show='*')
entry_usr_pwd.place(x=160, y=190)
# 登录方法函数
def usr_login():
# 获得name和password
usr_name = var_usr_name.get()
usr_pwd = var_usr_pwd.get()
try:
with open('usrs_info.pickle','rb') as usr_file:
usrs_info = pickle.load(usr_file)
except FileNotFoundError:
with open('usrs_info.pickle', 'wb') as usr_file:
usrs_info = {'admin': 'admin'}
pickle.dump(usrs_info, usr_file)
if usr_name in usrs_info:
if usr_pwd == usrs_info[usr_name]:
tkinter.messagebox.showinfo(title='欢迎', message='你是? ' + usr_name)
else:
tkinter.messagebox.showinfo(message='不好意思输入错误')
else:
is_sing_up=tkinter.messagebox.askyesno('第一次来请注册吧')
if is_sing_up:
use_sign_up()
# 取消登录的方法
def usr_sign_up():
def sign_to_Mofan_Python():
np = new_pwd.get()
npf = new_pwd_confirm.get()
nn = new_name.get()
with open('usrs_info.pickle', 'rb') as usr_file:
exits_usr_info=pickle.load(usr_file)
if np != npf:
tkinter.messagebox.showerror('密码输入错误')
elif nn in exits_usr_info:
tkinter.messagebox.showerror('用户名输入错误')
else:
exits_usr_info[nn] = np
with open('usrs_info.pickle', 'wb') as usr_file:
pickle.dump(exits_usr_info,usr_file)
tkinter.messagebox.showerror('欢迎','你已经注册成功')
window_sign_up.destroy()
window_sign_up = tk.Toplevel(window)
window_sign_up.geometry('350x200')
window_sign_up.title('注册窗口')
new_name = tk.StringVar()
new_name.set('example@qq.com')
tk.Label(window_sign_up,text='用户名').place(x=10,y=50)
entry_new_name = tk.Entry(window_sign_up, textvariable=new_name)
entry_usr_pwd.place(x=150, y=50)
new_pwd_confirm = tk.StringVar()
tk.Label(window_sign_up,text='密码:').place(x=10,y=90)
entry_usr_pwd = tk.Entry(window_sign_up, textvariable=new_pwd, show='*')
entry_usr_pwd.place(x=150, y=50)
new_pwd_confirm = tk.StringVar()
tk.Label(window_sign_up, text='Confirm password: ').place(x=10, y= 90)
entry_usr_pwd_confirm = tk.Entry(window_sign_up, textvariable=new_pwd_confirm, show='*')
entry_usr_pwd_confirm.place(x=150, y=90)
btn_comfirm_sign_up = tk.Button(window_sign_up, text='Sign up', command=sign_to_Mofan_Python)
btn_comfirm_sign_up.place(x=150, y=130)
# 登录窗口buttion
btn_login = tk.Button(window, text='Login', command=usr_login)
btn_login.place(x=170, y=230)
btn_sign_up = tk.Button(window, text='Sign up', command=usr_sign_up)
btn_sign_up.place(x=270, y=230)
window.mainloop()
网友评论