GUI登录窗口3(程序备份)

import random
from tkinter import *
from tkinter import messagebox
import string


# 注册属性
def name_12():
    # 账号输入框字符数量限制
    def limit_entry11_length(*args):
        value = entry11_var.get()
        if len(value) > 25:
            entry11_var.set(value[:25])
        else:
            # 检查特殊字符
            if not all(char in allowed_chars for char in value):
                #entry11_var.set('')  # 清空输入
                pad_zc_zh.place(x=935, y=225)
            else:
                pad_zc_zh.place_forget()

    # 密码输入框字符数量限制
    def limit_entry21_length(*args):
        value = entry21_var.get()
        if len(value) > 25:
            entry21_var.set(value[:25])
        else:
            # 检查特殊字符
            if not all(char in allowed_chars for char in value):
                #entry21_var.set('')  # 清空输入
                pad_zc_mm1.place(x=840, y=285)
            else:
                pad_zc_mm1.place_forget()

    # 确认密码输入框字符数量限制
    def limit_entry31_length(*args):
        value = entry31_var.get()
        if len(value) > 25:
            entry31_var.set(value[:25])
        else:
            # 检查特殊字符
            if not all(char in allowed_chars for char in value):
                #entry31_var.set('')  # 清空输入
                pad_zc_mm2.place(x=840, y=345)
            else:
                pad_zc_mm2.place_forget()

    # 验证码输入框字符数量限制
    def limit_entry41_length(*args):
        value = entry41_var.get() 
        if len(value) > 6:
            entry41_var.set(value[:6])
        else:
            # 检查特殊字符
            if not all(char in allowed_chars for char in value):
                #entry41_var.set('')  # 清空输入
                pad_zc_yzm.place(x=925, y=400)
            else:
                pad_zc_yzm.place_forget()

    # 密码显示隐藏
    def zc_cs1():
        global gif1, cs_pd1
        if cs_pd1 == 0:
            entry21.config(show="*")
            gif1 = PhotoImage(file=r"D:\GUI测试\image\dl_yc_1_1.gif")
            xyj_1.config(image=gif1)
            cs_pd1 = 1
        elif cs_pd1 == 1:
            entry21.config(show="")
            gif1 = PhotoImage(file=r"D:\GUI测试\image\dl_xs_1_1.gif")
            xyj_1.config(image=gif1)
            cs_pd1 = 0

    # 确认密码显示隐藏
    def zc_cs2():
        global gif2, cs_pd2
        if cs_pd2 == 0:
            entry31.config(show="*")
            gif2 = PhotoImage(file=r"D:\GUI测试\image\dl_yc_1_1.gif")
            xyj_2.config(image=gif2)
            cs_pd2 = 1
        elif cs_pd2 == 1:
            entry31.config(show="")
            gif2 = PhotoImage(file=r"D:\GUI测试\image\dl_xs_1_1.gif")
            xyj_2.config(image=gif2)
            cs_pd2 = 0

    # 登录界面隐藏
    button1.place_forget()
    entry1.place_forget()
    button2.place_forget()
    entry2.place_forget()
    button3.place_forget()
    entry3.place_forget()
    entry4.place_forget()
    bt1.place_forget()
    bt2.place_forget()
    an_nu.place_forget()
    hx_1.place_forget()
    hx_2.place_forget()
    hx_3.place_forget()
    xs_mm_dl.place_forget()

    # 生成验证码
    list11 = "1234567890"
    list21 = "abcdefghijklmnopqrstuvwxyz"
    list31 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    a11 = random.randint(0, 9)
    b11 = random.randint(0, 25)
    c11 = random.randint(0, 25)
    a111 = random.randint(0, 9)
    b111 = random.randint(0, 25)
    c111 = random.randint(0, 25)

    name_pd1 = list11[a11] + list21[b11] + list31[c11] + list11[a111] + list21[b111] + list31[c111]

    # 注册界面ui
    # 账号
    hx1_1 = Label(root, text="_" * 85, bg='White')
    button11 = Label(root, text="账号:", font=("幼圆", 15), bg='White')
    entry11_var = StringVar()
    entry11_var.trace("w", limit_entry11_length)
    entry11 = Entry(root, width=30, bg="White", bd=0, font=("微软雅黑",15), textvariable=entry11_var)
    # 密码
    hx1_2 = Label(root, text="_" * 85, bg='White')
    button21 = Label(root, text="密码:", font=("幼圆", 15), bg='White')
    entry21_var = StringVar()
    entry21_var.trace("w", limit_entry21_length)
    entry21 = Entry(root, width=30, bg="White", show="*", bd=0, font=("微软雅黑",15), textvariable=entry21_var)
    # 确认密码
    hx1_3 = Label(root, text="_" * 85, bg='White')
    button31 = Label(root, text="确认密码:", font=("幼圆", 15), bg='White')
    entry31_var = StringVar()
    entry31_var.trace("w", limit_entry31_length)
    entry31 = Entry(root, width=30, bg="White", show="*", bd=0, font=("微软雅黑",15), textvariable=entry31_var)
    gif1 = PhotoImage(file=r"D:\GUI测试\image\dl_yc_1_1.gif")
    xyj_1 = Button(root, image=gif1, height=16, width=16, bd=0, command=zc_cs1)
    # 密码显示隐藏按钮
    if gif1:
        xyj_1.config(image=gif1)
        root.xyj_1 = gif1
    gif2 = PhotoImage(file=r"D:\GUI测试\image\dl_yc_1_1.gif")
    xyj_2 = Button(root, image=gif2, height=16, width=16, bd=0, command=zc_cs2)
    if gif2:
        xyj_2.config(image=gif2)
        root.xyj_2 = gif2

    # 验证码
    hx1_4 = Label(root, text="_" * 85, bg='White')
    button41 = Label(root, text="验证码:", font=("幼圆", 15), bg='White')
    entry41_var = StringVar()
    entry41_var.trace("w", limit_entry41_length)
    entry41 = Entry(root, width=20, bg="White",bd=0, font=("微软雅黑",15), textvariable=entry41_var)
    entry51 = Entry(root, width=11, bg="White",bd=1, font=("微软雅黑",15))
    entry51.insert(0, name_pd1)
    entry51.config(state='readonly')

    pad_zc_zh = Label(root, text="账号只能包含字母和数字!", bg="White", fg="red")
    pad_zc_mm1 = Label(root, text="密码只能包含字母、数字和指定的特殊字符!", bg="White", fg="red")
    pad_zc_mm2 = Label(root, text="密码只能包含字母、数字和指定的特殊字符!", bg="White", fg="red")
    pad_zc_yzm = Label(root, text="验证码只能包含字母和数字!", bg="White", fg="red")

    button11.place(x=580, y=192)
    entry11.place(x=650, y=190)

    button21.place(x=580, y=252)
    entry21.place(x=650, y=250)
    xyj_1.place(x=1045, y=256)

    button31.place(x=540, y=312)
    entry31.place(x=650, y=310)
    xyj_2.place(x=1045, y=316)

    hx1_1.place(x=645, y=205)
    hx1_2.place(x=645, y=265)
    hx1_3.place(x=645, y=325)
    hx1_4.place(x=645, y=380)
    button41.place(x=560, y=367)
    entry41.place(x=650, y=365)

    entry51.place(x=930, y=365)

    # 注册判断
    def name_121():
        global zh_cc, mm_cc, zhk_zd, sj_cc
        mm_zh = entry11.get()
        mm_1 = entry21.get()
        mm_2 = entry31.get()
        mm_zh_tj = len(mm_zh)
        mm_1_tj = len(mm_1)
        mm_2_tj = len(mm_2)

        yzm_1_1 = entry41.get()
        sl_pd_1 = len(yzm_1_1)
        print(yzm_1_1)

        if mm_zh_tj == 0 and mm_1_tj == 0:
            messagebox.showerror("错误提示!", "账号和密码不得为空!")
        elif mm_zh_tj == 0:
            messagebox.showerror("错误提示!", "账号不得为空!")
        elif mm_1_tj == 0 or mm_2_tj == 0:
            messagebox.showerror("错误提示!", "密码不得为空!")
        else:
            if mm_1 != mm_2:
                messagebox.showerror("错误提示!", "两次密码不相同!")
            else:
                if yzm_1_1 != name_pd1:
                    if sl_pd_1 >= 1:
                        messagebox.showerror("错误提示!", "验证码错误!\n正确验证码为:" + name_pd1)
                    else:
                        messagebox.showerror("错误提示!", "验证码验证码不得为空!")
                else:
                    if mm_zh_tj > 7:
                        messagebox.showerror("错误提示!", "用户名最多不得超过七个字!")
                    elif mm_1_tj < 7 or mm_1_tj > 15:
                        messagebox.showerror("错误提示!", "密码数量介于7-15之间!")
                    elif mm_2_tj < 7 or mm_2_tj > 15:
                        messagebox.showerror("错误提示!", "密码数量介于7-15之间!")
                    else:
                        if mm_1 == mm_2 and yzm_1_1 == name_pd1:
                            if mm_zh in sj_cc:
                                messagebox.showerror("错误提示!", "该账号已被注册!")
                                entry11.delete(0, END)
                                entry21.delete(0, END)
                                entry31.delete(0, END)
                                entry41.delete(0, END)
                            else:
                                zhk_pd = messagebox.askquestion("注册完成","是否将账号:\n" + mm_zh + "\n密码:\n" + mm_1 + '\n账号/添加进账号库!')
                                if zhk_pd == "yes":
                                    messagebox.showinfo("注册成功!", "账号:\n" + mm_zh + "\n密码:\n" + mm_1 + '\n账号/密码已被添加进注册库!')
                                    zh_cc = mm_zh
                                    mm_cc = mm_1
                                    zhk_zd[zh_cc] = mm_cc
                                    sj_cc.append(zh_cc)

                                    entry11.delete(0, END)
                                    entry21.delete(0, END)
                                    entry31.delete(0, END)
                                    entry41.delete(0, END)
                                if zhk_pd == "no":
                                    messagebox.showwarning("取消信息","账号密码已被取消添加到账号库!")

        print(zhk_zd)
        print(sj_cc)
        print(mm_zh)
        print(mm_1)
        print(mm_2)

    # 返回登陆界面
    def name_111():
        global gif, xs_pd
        button11.place_forget()
        entry11.place_forget()
        button21.place_forget()
        entry21.place_forget()
        button31.place_forget()
        entry31.place_forget()
        button41.place_forget()
        entry41.place_forget()
        entry51.place_forget()
        bt11.place_forget()
        bt21.place_forget()
        xyj_2.place_forget()
        xyj_1.place_forget()
        hx1_1.place_forget()
        hx1_2.place_forget()
        hx1_3.place_forget()
        hx1_4.place_forget()

        button1.place(x=580, y=192)
        entry1.place(x=650, y=190)

        button2.place(x=580, y=252)
        entry2.place(x=650, y=250)

        button3.place(x=560, y=312)
        entry3.place(x=650, y=310)
        entry4.place(x=930, y=310)

        bt1.place(x=650, y=400)
        bt2.place(x=930, y=400)

        an_nu.place(x=650, y=355)
        xs_mm_dl.place(x=1045, y=256)

        hx_1.place(x=645, y=205)
        hx_2.place(x=645, y=265)
        hx_3.place(x=645, y=325)

        entry1.delete(0, END)
        entry2.delete(0, END)
        entry3.delete(0, END)
        entry2.config(show="*")
        xs_pd = 1
        gif = PhotoImage(file=r"D:\GUI测试\image\dl_yc_1_1.gif")
        xs_mm_dl.config(image=gif)

    bt11 = Button(root, text='登录', command=name_111, width=20, height=2, bd=0, bg="limegreen", fg='White', activebackground="lightgreen", activeforeground='White')
    bt21 = Button(root, text='注册', command=name_121, width=20, height=2, bd=0, bg="limegreen", fg='White', activebackground="lightgreen", activeforeground='White')
    bt11.place(x=650, y=470)
    bt21.place(x=930, y=470)

# 登录判断
def name_11():
    yzm_1 = entry3.get()
    sl_pd = len(yzm_1)
    print(yzm_1)
    dl_zh = entry1.get()
    dl_mm = entry2.get()
    dl_zh_pd = len(dl_zh)
    dl_mm_pd = len(dl_mm)
    print(dl_zh)
    print(dl_mm)
    if dl_zh_pd == 0 and dl_mm_pd == 0:
        messagebox.showerror("错误提示!", "账号密码不得为空!")
    elif dl_zh_pd == 0:
        messagebox.showerror("错误提示!", "账号不得为空!")
    elif dl_mm_pd == 0:
        messagebox.showerror("错误提示!", "密码不得为空!")
    else:
        if yzm_1 != name_pd:
            if sl_pd >= 1:
                messagebox.showerror("错误提示!", "验证码错误!\n正确验证码为:"+name_pd)
            else:
                messagebox.showerror("错误提示!", "验证码验证码不得为空!")
        else:
            if dl_zh not in sj_cc and dl_mm != zhk_zd[dl_zh]:
                messagebox.showerror("错误提示!", "账号密码不正确!")
            elif dl_zh not in sj_cc:
                messagebox.showerror("错误提示!", "账号不正确!")
            elif dl_mm != zhk_zd[dl_zh]:
                messagebox.showerror("错误提示!", "密码不正确!")
            elif dl_zh in sj_cc and dl_mm == zhk_zd[dl_zh] and yzm_1 == name_pd:
                if shu_x.get() != 1:
                    messagebox.showerror("错误提示!", "请勾选用户协议!")
                else:
                    messagebox.showinfo("登录成功!", "登录成功!")
                    print("登录成功!")
                    root.destroy()

# 生成验证码(点击按钮生成,保留待用)
def name_11_sl():
    list1_dl = "1234567890"
    list2_dl = "abcdefghijklmnopqrstuvwxyz"
    list3_dl = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    a_dl = random.randint(0, 9)
    b_dl = random.randint(0, 25)
    c_dl = random.randint(0, 25)
    a1_dl = random.randint(0, 9)
    b1_dl = random.randint(0, 25)
    c1_dl = random.randint(0, 25)

    name_pd_dl = list1_dl[a_dl] + list2_dl[b_dl] + list3_dl[c_dl] + list1_dl[a1_dl] + list2_dl[b1_dl] + list3_dl[c1_dl]
    entry4.insert(0, name_pd_dl)
    entry4.config(state='readonly')

# 密码显示隐藏转换
def yc_cs():
    global gif, xs_pd
    if xs_pd == 0:
        entry2.config(show="*")
        gif = PhotoImage(file=r"D:\GUI测试\image\dl_yc_1_1.gif")
        xs_mm_dl.config(image=gif)
        xs_pd = 1
    elif xs_pd == 1:
        entry2.config(show="")
        gif = PhotoImage(file=r"D:\GUI测试\image\dl_xs_1_1.gif")
        xs_mm_dl.config(image=gif)
        xs_pd = 0

# 变量部分
x = 1
xs_pd = 1
cs_pd1 = 1
cs_pd2 = 1
zhk_zd = {'123': "456"}
sj_cc = ["123"]
zh_cc = ""
mm_cc = ""

# 定义允许的字符集
allowed_special_chars = "!@#$%^&*()-_=+[]{}|;:'\",.<>?"
allowed_chars = set(string.ascii_letters + string.digits + allowed_special_chars)

# 窗口设置
root = Tk()

root.title("这是一个测试界面")
root.geometry("1200x720+390+150")
root.resizable(False, False)
root.config(bg="white")
root.overrideredirect(True)
root.attributes("-topmost",True) #将窗口保持最前
close_button = Button(root, text="✕", command=root.destroy, bg="White", bd=0, font=20, activeforeground="salmon")
close_button.place(x=1150,y=10)


# 窗口移动
'''def tu_d1(event):
    global tu_dox
    global tu_doy
    tu_dox = event.x
    tu_doy = event.y


def tu_d2(event):
    root.geometry(f'+{event.x_root - tu_dox}+{event.y_root - tu_doy}')

root.bind('', tu_d1)
root.bind('', tu_d2)'''

# 账号输入框字符数量限制
def limit_entry1_length(*args):
    value = entry1_var.get()
    if len(value) > 25:
        entry1_var.set(value[:25])
    else:
        # 检查特殊字符
        if not all(char in allowed_chars for char in value):
            #entry1_var.set('')  # 清空输入
            pad_zh.place(x=935, y=225)
        else:
            pad_zh.place_forget()
# 密码输入框字符数量限制
def limit_entry2_length(*args):
    value = entry2_var.get()
    if len(value) > 25:
        entry2_var.set(value[:25])
    else:
        # 检查特殊字符
        if not all(char in allowed_chars for char in value):
            #entry2_var.set('')  # 清空输入
            pad_mm.place(x=840, y=285)
        else:
            pad_mm.place_forget()

# 验证码输入框字符数量限制
def limit_entry3_length(*args):
    value = entry3_var.get()
    if len(value) > 6:
        entry3_var.set(value[:6])
    else:
        # 检查特殊字符
        if not all(char in allowed_chars for char in value):
            #entry3_var.set('')  # 清空输入
            pad_yzm.place(x=925, y=345)
        else:
            pad_yzm.place_forget()

# 登陆界面验证码生成部分
list1 = "1234567890"
list2 = "abcdefghijklmnopqrstuvwxyz"
list3 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
a = random.randint(0, 9)
b = random.randint(0, 25)
c = random.randint(0, 25)
a1 = random.randint(0, 9)
b1 = random.randint(0, 25)
c1 = random.randint(0, 25)

name_pd = list1[a]+list2[b]+list3[c]+list1[a1]+list2[b1]+list3[c1]

# 登录界面ui
hx_1 = Label(root,text="_"*85, bg='White')
button1 = Label(root, text="账号:", font=("幼圆", 15), bg='White')
entry1_var = StringVar()
entry1_var.trace("w", limit_entry1_length)
entry1 = Entry(root, width=30, bg="White", bd=0, font=("微软雅黑",15), textvariable=entry1_var)
# 密码
hx_2 = Label(root,text="_"*85, bg='White')
button2 = Label(root, text="密码:", font=("幼圆", 15), bg='White')
entry2_var = StringVar()
entry2_var.trace("w", limit_entry2_length)
entry2 = Entry(root, width=30, show="*", bg="White", bd=0, font=("微软雅黑",15), textvariable=entry2_var)
# 验证码
hx_3 = Label(root,text="_"*85, bg='White')
button3 = Label(root, text="验证码:", font=("幼圆", 15), bg='White')
entry3_var = StringVar()
entry3_var.trace("w", limit_entry3_length)
entry3 = Entry(root, width=20, bg="White", bd=0, font=("微软雅黑",15), textvariable=entry3_var)
entry4 = Entry(root, width=11, bg="White", bd=1, font=("微软雅黑",15))
entry4.insert(0, name_pd)
entry4.config(state='readonly')

hx_1.place(x=645, y=205)
hx_2.place(x=645, y=265)
hx_3.place(x=645, y=325)

# 密码显示隐藏按钮
gif = PhotoImage(file=r"D:\GUI测试\image\dl_yc_1_1.gif")
xs_mm_dl = Button(root, image=gif, height=16, width=16,bg="White",bd=0, command=yc_cs)

gif3 = PhotoImage(file=r"D:\GUI测试\image\bg.gif")
xs_mm_dl1 = Button(root, image=gif3, height=720, width=500,bg="White",bd=0, state=DISABLED)


# 登录服务按钮
shu_x = IntVar()
an_nu = Checkbutton(root,text="是否同意账号登陆服务", variable=shu_x,bg = "White", bd=1)

# 登录注册按钮
bt1 = Button(root, text='登   录', command=name_11, width=20, height=2, bd=0, bg="limegreen", fg='White', activebackground="lightgreen", activeforeground='White')
bt2 = Button(root, text='注   册', command=name_12, width=20, height=2, bd=0, bg="limegreen", fg='White', activebackground="lightgreen", activeforeground='White')

# 位置部分
button1.place(x=580, y=192)
entry1.place(x=650, y=190)

button2.place(x=580, y=252)
entry2.place(x=650, y=250)

button3.place(x=560, y=312)
entry3.place(x=650, y=310)
entry4.place(x=930, y=310)

an_nu.place(x=650,y=355)

bt1.place(x=650, y=400)
bt2.place(x=930, y=400)

xs_mm_dl.place(x=1045, y=256)
xs_mm_dl1.place(x=0,y=0)

pad_zh = Label(root,text="账号只能包含字母和数字!",bg = "White",fg="red")
pad_mm = Label(root,text="密码只能包含字母、数字和指定的特殊字符!",bg = "White",fg="red")
pad_yzm = Label(root,text="验证码只能包含字母和数字!",bg = "White",fg="red")



root.mainloop() # 窗口循环



你可能感兴趣的:(GUI登录窗口,GUI开发,python,开发语言)