Python全局变量充当局部变量

时间:2019-06-01 18:46:16

标签: python function variables global-variables local-variables

所以我正在使用tkinter和密码学在python中建立一个注册登录系统。我几乎粘贴了所有代码,但您只需要关注 user_register()和user_login()。 在user_register中,f_1_user_register(示例)设置为global,但是我无法在user_login()中访问它。我想访问在user_login()中的user_register()中设置为global的所有内容。 我按照本教程中的“基础”进行操作,更改了大部分代码:https://www.youtube.com/watch?v=Xt6SqWuMSA8&

def user_register():

    global username_user_register
    global password_user_register
    global password_encrypt_user_register
    global f_1_user_register
    global key_1_user_register

    path_1 = os.getcwd()
    username_user_register = username_register_screen.get()
    password_user_register = password_register_screen.get()

    if (
            not username_user_register.isalnum() or not len(username_user_register) >= 2 <= 26
            or not password_user_register.isalnum() or not len(password_user_register) >= 8 <= 26
    ):

        username_or_password_does_not_meet_requirements_label_user_register = Label(
            screen_register, text="Error 1", bg="red", fg="white", width="18", height="2",
            font=("Calibri", 14)
        ).grid(row=6, column=0, padx=5, pady=5)

        username_entry_register_screen.delete(0, END)
        password_entry_register_screen.delete(0, END)

    else:
        username_test_user_register = os.path.exists(path_1 + "\\Accounts\\" + username_user_register + ".txt")
        if username_test_user_register:

            username_exists_label_user_register = Label(
                screen_register, text="Error 2", bg="red", fg="white", width="18", height="2",
                font=("Calibri", 14)
            ).grid(row=6, column=0, padx=5, pady=5)

            username_entry_register_screen.delete(0, END)
            password_entry_register_screen.delete(0, END)

        else:

            key_1_user_register = Fernet.generate_key()
            f_1_user_register = Fernet(key_1_user_register)
            password_encode_user_register = password_user_register.encode()
            password_encrypt_user_register = f_1_user_register.encrypt(password_encode_user_register)

            user_file_user_register = open(path_1 + "\\Accounts\\" + username_user_register + ".txt", "w")
            user_file_user_register.write(username_user_register)
            user_file_user_register.write("\n" + str(password_encrypt_user_register))
            user_file_user_register.write("\n" + str(f_1_user_register))
            user_file_user_register.close()

            username_entry_register_screen.delete(0, END)
            password_entry_register_screen.delete(0, END)

            registration_succesfull_label_user_register = Label(
                screen_register, text="Registration Successful", bg="green", fg="white", width="18", height="2",
                font=("Calibri", 14)
            ).grid(row=6, column=0, padx=5, pady=5)


def user_login():

    username_user_login = username_login_screen.get()
    password_user_login = password_login_screen.get()

    path_2 = os.getcwd()
    username_test_user_login = os.path.exists(path_2 + "\\Accounts\\" + username_user_login + ".txt")
    if not username_test_user_login:
        username_exists_label_user_login = Label(
            screen_register, text="Error 3", bg="red", fg="white", width="18", height="2",
            font=("Calibri", 14)
        ).grid(row=6, column=0, padx=5, pady=5)

        username_entry_login_screen.delete(0, END)
        password_entry_login_screen.delete(0, END)

    else:
        user_file_login = open(path_2 + "\\Accounts\\" + username_user_login + ".txt", "r")
        password_test_user_file_user_login = user_file_login.read(1)
        f_2 = key_1_user_reg


def register_screen():

    global screen_register
    global username_register_screen
    global password_register_screen
    global username_entry_register_screen
    global password_entry_register_screen

    screen_register = Toplevel(screen)
    screen_register.title("RegisterScreen")
    screen_register.geometry("620x620")
    screen_register.configure(bg="dodger blue")

    username_register_screen = StringVar()
    password_register_screen = StringVar()

    title_label_register_screen = Label(
        screen_register, text="REGISTER", bg="royal blue", fg="white", width="18", height="2", font=("Calibri", 48)
    ).grid(row=0, column=0, padx=10, pady=10)

    username_label_register_screen = Label(
        screen_register, text="USERNAME", bg="royal blue", fg="white", width="18", height="2", font=("Calibri", 18)
    ).grid(row=1, column=0, padx=5, pady=5)

    username_entry_register_screen = Entry(
        screen_register, font=("Calibri", 18), textvariable=username_register_screen
    )
    username_entry_register_screen.grid(row=2, column=0, padx=5, pady=5)

    password_label_register_screen = Label(
        screen_register, text="PASSWORD", bg="royal blue", fg="white", width="18", height="2", font=("Calibri", 18)
    ).grid(row=3, column=0, padx=5, pady=5)

    password_entry_register_screen = Entry(
        screen_register, font=("Calibri", 18), textvariable=password_register_screen
    )
    password_entry_register_screen.grid(row=4, column=0, padx=5, pady=5)

    continue_button_register_screen = Button(
        screen_register, text="CONTINUE", height="1", width="18", fg="white", bg="medium sea green",
        font=("Calibri", 26), command=user_register
    ).grid(row=5, column=0, padx=5, pady=5)


def login_screen():

    global screen_login
    global username_login_screen
    global password_login_screen
    global username_entry_login_screen
    global password_entry_login_screen

    screen_login = Toplevel(screen)
    screen_login.title("RegisterScreen")
    screen_login.geometry("620x620")
    screen_login.configure(bg="dodger blue")

    username_login_screen = StringVar()
    password_login_screen = StringVar()

    title_label_login_screen = Label(
        screen_login, text="LOGIN", bg="royal blue", fg="white", width="18", height="2", font=("Calibri", 48)
    ).grid(row=0, column=0, padx=10, pady=10)

    username_label_login_screen = Label(
        screen_login, text="USERNAME", bg="royal blue", fg="white", width="18", height="2", font=("Calibri", 18)
    ).grid(row=1, column=0, padx=5, pady=5)

    username_entry_login_screen = Entry(
        screen_login, font=("Calibri", 18), textvariable=username_login_screen
    )
    username_entry_login_screen.grid(row=2, column=0, padx=5, pady=5)

    password_label_login_screen = Label(
        screen_login, text="PASSWORD", bg="royal blue", fg="white", width="18", height="2", font=("Calibri", 18)
    ).grid(row=3, column=0, padx=5, pady=5)

    password_entry_login_screen = Entry(
        screen_login, font=("Calibri", 18), textvariable=password_login_screen
    )
    password_entry_login_screen.grid(row=4, column=0, padx=5, pady=5)

    continue_button_login_screen = Button(
        screen_login, text="CONTINUE", height="1", width="18", fg="white", bg="medium sea green",
        font=("Calibri", 26), command=user_login
    ).grid(row=5, column=0, padx=5, pady=5)


def main_screen():

    global screen

    screen = Tk()
    screen.geometry("620x620")
    screen.title("MainScreen")
    screen.configure(bg="dodger blue")

    title_label_main_screen = Label(
        text="MAIN SCREEN", bg="royal blue", fg="white", width="18", height="2", font=("Calibri", 48)
    ).grid(row=0, column=0, padx=10, pady=10)

    login_button_main_screen = Button(
        text="LOGIN", height="1", width="18", fg="white", bg="medium sea green", font=("Calibri", 26),
        command=login_screen
    ).grid(row=1, column=0, padx=10, pady=10)

    register_button_main_screen = Button(
        text="REGISTER", height="1", width="18", fg="white", bg="medium sea green", font=("Calibri", 26),
        command=register_screen
    ).grid(row=2, column=0, padx=10, pady=10)

    info_button_main_screen = Button(
        text="INFO", height="1", width="18", fg="white", bg="medium sea green", font=("Calibri", 26),
        command=info_screen
    ).grid(row=3, column=0, padx=10, pady=10)

    screen.mainloop()


main_screen()

0 个答案:

没有答案