tkinter登录窗口

tkinter登录窗口

#登录窗口
import tkinter as tk
import tkinter.messagebox 
import pickle
window = tk.Tk()
window.title('Welcome to python')
window.geometry('350x300')

tk.Label(window,text='User name:').place(x=50,y=100)
tk.Label(window,text='Password:').place(x=50,y=140)

var_user_name = tk.StringVar()
var_user_name.set("example@python.com")
var_user_password = tk.StringVar()
var_user_password.set("")
entry_user_name = tk.Entry(window,textvariable=var_user_name)
entry_user_name.place(x=160,y=100)
entry_user_password = tk.Entry(window,textvariable=var_user_password,show='*')
entry_user_password.place(x=160,y=140)

#login and sign up button
def user_login():
    user_name =var_user_name.get()
    user_pwd = var_user_password.get()
    try:
        with open('users_info.pickle','rb') as user_file:
            user_info = pickle.load(user_file)
    except FileNotFoundError:
        with open('users_info.pickle','wb') as user_file:
            user_info = {'admin':'admin'}
            pickle.dump(user_info,user_file)
    
    if user_name in user_info:
        if user_pwd ==user_info[user_name]:
            tk.messagebox.showinfo(title='Welcome',message='How are you '+user_name)
        else:
            tk.messagebox.showerror(message='Error,your password is wrong,try again')
    else:
        is_sign_up = tk.messagebox.askyesno('Welcome','You have not sign up yet.Sign up today?')
        if is_sign_up:
            user_sign_up()
            
def user_sign_up():
    def sign_to():
        np1 = new_user_name.get()
        np2 = new_user_password.get()
        np3 = new_conformed_user_password.get()
        with open('users_info.pickle','rb') as user_file:  #对比有没有重复注册过
            exist_user_info = pickle.load(user_file)
        if np2!=np3:
            tk.messagebox.showerror('Error','Password and confirm password must be same')
        elif np1 in exist_user_info:
            tk.messagebox.showerror('Error','The user has already signed up!')
        else:
            exist_user_info[np1] = np2
            with open('users_info.pickle','wb') as user_file:
                pickle.dump(exist_user_info,user_file)
            tk.messagebox.showinfo('Welcome','You have successfully signed up!')
            window_sign_up.destroy()
            
    window_sign_up = tk.Toplevel(window)
    window_sign_up.geometry('400x300')
    window_sign_up.title('Sign up window')
    
    tk.Label(window_sign_up,text='New User name:').place(x=20,y=80)
    tk.Label(window_sign_up,text='New Password:').place(x=20,y=120)
    tk.Label(window_sign_up,text='Confirmed Password:').place(x=20,y=160)

    new_user_name = tk.StringVar()
    new_user_name.set("example@python.com")
    new_user_password = tk.StringVar()
    new_user_password.set("")
    new_conformed_user_password = tk.StringVar()
    new_conformed_user_password.set("")
    entry_new_user_name = tk.Entry(window_sign_up,textvariable=new_user_name)
    entry_new_user_name.place(x=150,y=80)
    entry_new_user_password = tk.Entry(window_sign_up,textvariable=new_user_password,show='*')
    entry_new_user_password.place(x=150,y=120)
    entry_conformed_user_password = tk.Entry(window_sign_up,textvariable=new_conformed_user_password,show='*')
    entry_conformed_user_password.place(x=150,y=160)
    
    btn_sign_up = tk.Button(window_sign_up,text='Sign up',command=sign_to)
    btn_sign_up.place(x=150,y=220)
    
        
btn_login = tk.Button(window,text='Login',command=user_login)
btn_login.place(x=150,y=170)
btn_sign_up = tk.Button(window,text='Sign up',command=user_sign_up)
btn_sign_up.place(x=250,y=170)

window.mainloop()
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值