用户登陆界面

 

 

# coding=utf-8
import tkinter as tk
import pickle
from PIL import Image
from PIL import ImageTk
from tkinter import messagebox
import random
import string
import sys
import math
from PIL import  ImageDraw, ImageFont, ImageFilter

WIDTH= 400
HEIGHT = 300

window = tk.Tk()
window.title('welcome to xiaozhe')
window.geometry('400x300')


#welcome image
canvas = tk.Canvas(window,height=300,width=500)
image_file = Image.open('timg.jpeg')
image_file = image_file.resize((WIDTH,HEIGHT),Image.BILINEAR)
image_file = ImageTk.PhotoImage(image_file)
background = canvas.create_image(0,0,anchor = 'nw',image=image_file)
canvas.pack()


#user information
tk.Label(window,text = 'Username:',width=8).place(x=50,y=150)
tk.Label(window,text = 'Password:',width=8).place(x=50,y=190)

var_usr_name=tk.StringVar()
var_usr_name.set('example@qq.com')
var_usr_pwd=tk.StringVar()

entry_usr_name = tk.Entry(window,textvariable = var_usr_name)
entry_usr_name.place(x=160,y=150)

entry_usr_name = tk.Entry(window,textvariable = var_usr_pwd,show='*')
entry_usr_name.place(x=160,y=190)


#login and sign up button

def usr_login():
    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]:
            messagebox.showinfo(title='Welcome',message='How are you?'+usr_name)
        else:
            tk.messagebox.showerror(message='Error,you password is wrong,try again.')
    else:
        is_sign_up = tk.messagebox.askyesno('Welcome','You have not sign up yet.Sign up')
        if is_sign_up:
            usr_sign_up()

def usr_yanzheng():
    new_window = tk.Toplevel()

    s = gene_text()
    print(s)
    image1 = gene_code(s)
    image1 = image1.resize((300, 200), Image.BILINEAR)
    image1 = ImageTk.PhotoImage(image1)
    l1 = tk.Label(new_window, image=image1, bg='gray')
    l1.pack()



    def usr_modify_pwd():

        def sign_to_xiaozhe():
            np = new_pwd.get()
            npf = new_pwd_confirm.get()
            nn = new_name.get()
            with open('usrs_info.pickle','rb') as usr_file:
                exist_usr_info = pickle.load(usr_file)
            if np!=npf:
                tk.messagebox.showerror('Error','Password and confirm password must be the same!')
            elif nn not in exist_usr_info:
                tk.messagebox.showerror('Error','The user has not signed up!')
            else:
                exist_usr_info[nn]=np
                with open('usrs_info.pickle','wb') as usr_file:
                    pickle.dump(exist_usr_info,usr_file)
                tk.messagebox.showinfo('Welcome','you have sucessfully modified!')
                window_sign_up.destroy()

        if e1.get() == s:
            new_window.destroy()
            window_sign_up = tk.Toplevel(window)
            window_sign_up.geometry('350x200')
            window_sign_up.title('Modify window')

            new_name = tk.StringVar()
            new_name.set('example@qq.com')
            tk.Label(window_sign_up,text = 'Username:').place(x=10,y=10)
            entry_new_name = tk.Entry(window_sign_up,textvariable=new_name)
            entry_new_name.place(x=150,y=10)

            new_pwd = tk.StringVar()
            tk.Label(window_sign_up,text = 'Password:').place(x=10,y=50)
            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_com_sign_up = tk.Button(window_sign_up,text = 'Modify',command=sign_to_xiaozhe)
            btn_com_sign_up.place(x=150,y=130)
        else:
            tk.messagebox.showerror(message='Error,your yanzhengma is wrong.')


    yanzhengma = tk.StringVar()
    e1 = tk.Entry(new_window,textvariable=yanzhengma)
    e1.pack()
    b3 = tk.Button(new_window, text='confirm', command=usr_modify_pwd)
    b3.pack()

    new_window.mainloop()

def usr_sign_up():
    def sign_to_xiaozhe():
        np = new_pwd.get()
        npf = new_pwd_confirm.get()
        nn = new_name.get()
        with open('usrs_info.pickle','rb') as usr_file:
            exist_usr_info = pickle.load(usr_file)
        if np!=npf:
            tk.messagebox.showerror('Error','Password and confirm password must be the same!')
        elif nn in exist_usr_info:
            tk.messagebox.showerror('Error','The user has already signed up!')
        else:
            exist_usr_info[nn]=np
            with open('usrs_info.pickle','wb') as usr_file:
                pickle.dump(exist_usr_info,usr_file)
            tk.messagebox.showinfo('Welcome','you have sucessfully signed up!')
            window_sign_up.destroy()

    window_sign_up = tk.Toplevel(window)
    window_sign_up.geometry('350x200')
    window_sign_up.title('Sign up window')

    new_name = tk.StringVar()
    new_name.set('example@qq.com')
    tk.Label(window_sign_up,text = 'Username:').place(x=10,y=10)
    entry_new_name = tk.Entry(window_sign_up,textvariable=new_name)
    entry_new_name.place(x=150,y=10)

    new_pwd = tk.StringVar()
    tk.Label(window_sign_up,text = 'Password:').place(x=10,y=50)
    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_com_sign_up = tk.Button(window_sign_up,text = 'Sign up',command=sign_to_xiaozhe)
    btn_com_sign_up.place(x=150,y=130)

btn_login = tk.Button(window,text = 'Login',command = usr_login)
btn_login.place(x=160,y=230)

btn_sign_up = tk.Button(window,text = 'Sign up',command = usr_sign_up)
btn_sign_up.place(x=300,y=230)

btn_sign_up = tk.Button(window,text = 'forget',command = usr_yanzheng)
btn_sign_up.place(x=230,y=230)

#**************************************************************************

# 字体的位置,不同版本的系统会有不同
font_path = '/System/Library/Fonts/Helvetica.ttc'
# 生成几位数的验证码
number = 4
# 生成验证码图片的高度和宽度
size = (100, 30)
# 背景颜色,默认为白色
bgcolor = (255, 255, 255)
# 字体颜色,默认为蓝色
fontcolor = (0, 0, 255)
# 干扰线颜色。默认为红色
linecolor = (255, 0, 0)
# 是否要加入干扰线
draw_line = True
# 加入干扰线条数的上下限
line_number = (1, 5)


# 用来随机生成一个字符串
def gene_text():
    source = list(string.ascii_letters)
    for index in range(0, 10):
        source.append(str(index))
    return ''.join(random.sample(source, number))  # number是生成验证码的位数


# 用来绘制干扰线
def gene_line(draw, width, height):
    begin = (random.randint(0, width), random.randint(0, height))
    end = (random.randint(0, width), random.randint(0, height))
    draw.line([begin, end], fill=linecolor)


# 生成验证码
def gene_code(s):
    width, height = size  # 宽和高
    image = Image.new('RGBA', (width, height), bgcolor)  # 创建图片
    font = ImageFont.truetype(font_path, 25)  # 验证码的字体
    draw = ImageDraw.Draw(image)  # 创建画笔
    text = s  # 生成字符串
    font_width, font_height = font.getsize(text)
    draw.text(((width - font_width) / number, (height - font_height) / number), text,
              font=font, fill=fontcolor)  # 填充字符串
    if draw_line:
        gene_line(draw, width, height)
    # image = image.transform((width+30,height+10), Image.AFFINE, (1,-0.3,0,-0.1,1,0),Image.BILINEAR)  #创建扭曲
    image = image.transform((width + 20, height + 10), Image.AFFINE, (1, -0.3, 0, -0.1, 1, 0), Image.BILINEAR)  # 创建扭曲
    image = image.filter(ImageFilter.EDGE_ENHANCE_MORE)  # 滤镜,边界加强
    # image.save('idencode.png')  # 保存验证码图片
    return image

#**************************************************************************


window.mainloop()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值