thinter項目-登錄界面

源代碼:

import tkinter as tk
from tkinter import messagebox  # import this to fix messagebox error
import pickle


# 創建主窗口
window = tk.Tk()
window.title('歡迎來到登錄界面')
window.geometry('450x300')


# 添加背景圖
canvas = tk.Canvas(window, height=200, width=500)  # 創建畫布
image_file = tk.PhotoImage(file='feihao.png')  # 加載圖片文件
image = canvas.create_image(180, 20, anchor='nw', image=image_file)  # 將圖片置於畫布上
canvas.pack()  # 放置畫布


# 用戶信息
tk.Label(window, text='User name:').place(x=120, y=150)  # 創建一個`label`名爲`User name: `置於座標(120,150)
tk.Label(window, text='Password:').place(x=120, y= 190)
# 賬號密碼輸入框
var_usr_name = tk.StringVar()  # 定義變量,用於輸入用戶名
var_usr_name.set('write your name')  # 輸入用戶名窗口默認顯示'write your name'
entry_usr_name = tk.Entry(window, textvariable=var_usr_name)  # 創建輸入框
entry_usr_name.place(x=200, y=150)
var_usr_pwd = tk.StringVar()
entry_usr_pwd = tk.Entry(window, textvariable=var_usr_pwd, show='*')  # `show`這個參數將輸入的密碼變爲`***`的形式
entry_usr_pwd.place(x=200, y=190)


# 登錄功能
def usr_login():
    # 這兩行代碼就是獲取用戶輸入的`usr_name`和`usr_pwd`
    usr_name = var_usr_name.get()
    usr_pwd = var_usr_pwd.get()

    # 這裏設置異常捕獲,當我們第一次訪問用戶信息文件時是不存在的,所以這裏設置異常捕獲
    try:
        # 'rb'是打開文件爲以二進制格式“讀”,文件必須存在,否則會報錯
        with open('usrs_info.pickle', 'rb') as usr_file:
            usrs_info = pickle.load(usr_file)
    except FileNotFoundError:
# 這裏就是我們在沒有讀取到`usr_file`的時候,程序會創建一個`usr_file`這個文件,並將管理員
# 的用戶和密碼寫入,即用戶名爲`admin`密碼爲`admin`。
        with open('usrs_info.pickle', 'wb') as usr_file:
            usrs_info = {'admin': 'admin'}
            pickle.dump(usrs_info, usr_file)
    # 如果用戶名和密碼與文件中的匹配成功,則會登錄成功,並跳出彈窗`how are you?`加上你的用戶名
    if usr_name in usrs_info:
        if usr_pwd == usrs_info[usr_name]:
            # 登錄成功後的功能,可以改爲其它功能
            tk.messagebox.showinfo(title='Welcome', message='How are you?'+usr_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:
            usr_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:
            exist_usr_info = pickle.load(usr_file)
            # 這裏就是判斷,如果兩次密碼輸入不一致,則提示`'Error', 'Password and confirm password must be the same!'`
            if np != npf:
                tk.messagebox.showerror('Error', 'Password and confirm password must be the same!')
            # 如果用戶名已經在我們的數據文件中,則提示`'Error', 'The user has already signed up!'`
            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 successfully signed up!')
                window_sign_up.destroy()


    # 這裏就是在主體窗口的window上創建一個Sign up window窗口
    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('write your name')
    tk.Label(window_sign_up, text='User name: ').place(x=10, y= 10)  # 將`User name:`放置在座標(10,10)。
    entry_new_name = tk.Entry(window_sign_up, textvariable=new_name)  # 設置輸入姓名框
    entry_new_name.place(x=150, y=10)  # `entry`放置在座標(150,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)

    # 下面的 sign_to_Mofan_Python 我們再後面接着說
    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)


# 登錄和註冊按鈕
btn_login = tk.Button(window, text='log in', command=usr_login)  # 定義一個`button`按鈕,名爲`Login`,觸發命令爲`usr_login'
btn_login.place(x=270, y=230)
btn_sign_up = tk.Button(window, text='Sign up', command=usr_sign_up)
btn_sign_up.place(x=120, y=230)

window.mainloop()

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章