簡體   English   中英

Python - 在 tkinter gui 上需要幫助

[英]Python - Need help on tkinter gui

因此,我正在使用 Tkinter 制作登錄 gui,但遇到了障礙。 出於某種原因,當我登錄一個帳戶時,它會說找不到用戶名(我輸入了一個錯誤)然后它會說它已經登錄。我輸入了正確的用戶名和密碼,但它總是會在它說之前說未知用戶名它已登錄。

這是我的代碼:

from tkinter import *
import hashlib
import json

userdata_file = 'UserData.json'



class Login(Tk):
    def __init__(self):
        # Inherit from Tk parent class
        super(Login, self).__init__()

        # Setting up child class attributes
        self.geometry("750x500")
        self.title("Login Page")
        self.resizable(height = False, width = False)
        # self.iconbitmap(path to icon.ico) < setup an icon for the GUI
        self.pepper = "[]#'[]''332#4'32'4#324'"
        self.salt = "']2[3][2#3['2]['3]['#']["
        self.hasher = lambda text: hashlib.sha256((self.pepper, text, self.salt).encode()).hexdigest()
        self.usernm = StringVar()
        self.passwd = StringVar()

        # Main interface
        C = Canvas(self, height = 250, width = 300)
        filename = PhotoImage(file = "wallpaper.png")
        background_label = Label(self, image = filename)
        background_label.place(x = 0, y = 0, relwidth = 1, relheight = 1)
        C.pack()



        login_button = PhotoImage(file = "login_button.png")
        signup_button = PhotoImage(file = "signup_button.png")
        Entry(textvariable = self.usernm, font=("Arial", 15), bg="#454545", border=0).place(x = 200, y = 195, height = 40, width = 330)
        Entry(textvariable = self.passwd, font=("Arial", 15), bg="#454545", show='•', border=0).place(x = 200, y = 278, height = 40, width = 330)
        B1 = Button(command = self.login, image=login_button, bg="black", border=0, activebackground="black").place(x = 186, y = 353, height = 52, width = 146)
        login_error = Label(text="Login error", font=("Arial", 12), fg="black", border=0, bg='black').place(x = 290, y = 330, height = 21, width = 146)
        B2 = Button(command = self.signup, image=signup_button, bg="black", border=0, activebackground="black").place(x = 351, y = 353, height = 52, width = 146)
        self.login_error = login_error
        self.mainloop()


**# The main problem appears in the following methods.**


    def login(self):
        # Creates a variable of what the user entered
        self.username = self.usernm.get()
        self.password = self.passwd.get()
        with open(userdata_file) as data:
            userdata = json.load(data)
            for profile in userdata:
                if self.username == profile['Username']:
                    if self.password == profile['Password']:
                        print(f'Successfully logged into {self.username}\'s account.')
                    else:
                        print("Incorrect password.")
                else:
                    print('Username not found.')



    def signup_page(self):
        pass  # Signup page goes here

    def signup(self):  # Delete this function once signup page is made and let me know -Sticky
        self.username = self.usernm.get()
        self.password = self.passwd.get()
        can_create = False
        with open(userdata_file) as data:
            userdata = json.load(data)
            for profile in userdata:
                if self.username == profile['Username']:
                    print('Username Taken')
                if not self.username == profile['Username']:
                    print('User created')






if __name__ == "__main__":
    # Starts if the program is not imported
    login = Login()

如果您知道任何可以以任何方式幫助我的事情,請告訴我,因為過去 2 天我一直被困住。

您的代碼有一些問題,例如self.login_errorNone 解決方案和解釋可以在這里找到。

對於您的登錄 function,您遍歷所有已注冊的人並檢查它是否與該人的登錄詳細信息匹配(順便說一句,您在檢查之前忘記了 hash 密碼)。 問題是每次您找不到具有該用戶名+密碼的人時,它都會打印出“找不到用戶名”。 並轉到下一個人的詳細信息。

將 function 更改為:

def login(self):
    # Creates a variable of what the user entered
    username = self.usernm.get()
    password = self.passwd.get()
    password = self.hasher(password) # Hash the password
    found = False # Add a flag to check if the username matches
    with open(userdata_file) as data:
        userdata = json.load(data)
    # Close the file before looping
    for profile in userdata:
        if self.username == profile['Username']:
            found = True
            if self.password == profile['Password']:
                print(f'Successfully logged into {self.username}\'s account.')
                break # Stop the for loop
            else:
                print("Incorrect password.")
                break # Stop the for loop
    if not found: # If the username hasn't been found in the file
        print('Username not found.')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM