简体   繁体   中英

Function's If/else statement not working

I'm trying to get a "Login Success/Fail" text appear when we click on the login button if the user and the password are equal.

The problem I am having is that no matter what the user and password we write it always display the "success" message instead of the fail one.

I am using the .get to get the input of the user and saving on a variable.

Heres my code:

from tkinter import *

root = Tk()

label_name = Label(root, text="Username", fg="black")
label_password = Label(root, text="Password", fg="black")
input_name = Entry(root)
name = input_name.get()
input_password = Entry(root, show="*")
password = input_password.get()

label_name.grid(row=0, column=0, sticky=E)
label_password.grid(row=1, column=0, sticky=E)

input_name.grid(row=0, column=1)
input_password.grid(row=1, column=1)

checkbox = Checkbutton(root, text="Remember me")
checkbox.grid(columnspan=2, sticky=W)

login_button = Button(root, text="Login")
login_button.grid(row=1, column=2)


login_success = Label(root, text="Welcome", fg="black")
login_fail = Label(root, text="login fail", fg="black")


def login(event):
    if name == password:
        for widget in root.winfo_children():
            widget.grid_forget()
        login_true = Label(root, text="Welcome", fg="black")
        login_true.grid()
    elif name != password:
        for widget in root.winfo_children():
            widget.grid_forget()
        login_false = Label(root, text="login fail", fg="black")
        login_false.grid()


login_button.bind("<Button-1>", login)

root.mainloop()

EDIT: I also used the "command" attribute but it had the same result:

def login():
    name = input_name.get()
    password = input_password.get()
    if name == password:
        for widget in root.winfo_children():
            widget.grid_forget()
        login_true = Label(root, text="Welcome", fg="black")
        login_true.grid()
    elif name != password:
        for widget in root.winfo_children():
            widget.grid_forget()
        login_false = Label(root, text="login fail", fg="black")
        login_false.grid()


login_button = Button(root, text="Login", command=login)
login_button.grid(row=1, column=2)


root.mainloop()

EDIT2: Fixed by adding an new condition

def login():

    if input_name.get() == "" or input_password.get() == "":
        for widget in root.winfo_children():
            widget.grid_forget()
        login_false = Label(root, text="login fail", fg="black")
        login_false.grid()
    elif input_name.get() == input_password.get():
        for widget in root.winfo_children():
            widget.grid_forget()
        login_true = Label(root, text="Welcome", fg="black")
        login_true.grid()
    elif input_name.get() != input_password.get():
        for widget in root.winfo_children():
            widget.grid_forget()
        login_false = Label(root, text="login fail", fg="black")
        login_false.grid()

The reason why it always returns true is because you have declared name and password in two local namespaces. One in the main body of your code and one within the function login() . name and password within the login() function both equal None because they never have an assigned value. Meaning that they are always equal and will always return True .

Change the function to the below:

def login():
    if input_name.get() == input_password.get():
        for widget in root.winfo_children():
            widget.grid_forget()
        login_true = Label(root, text="Welcome", fg="black")
        login_true.grid()
    elif input_name.get() != input_password.get():
        for widget in root.winfo_children():
            widget.grid_forget()
        login_false = Label(root, text="login fail", fg="black")
        login_false.grid()

Using .bind() on a Button widget for <Button-1> tends to be a terrible idea. Button widgets have a built in attribute called command which can be used to called get a callback when clicked:

from tkinter import *

root = Tk()

def login():
    print("Login triggered")

login_button = Button(root, text="Login", command=login)
login_button.pack()

root.mainloop()

On a side note, if you need to pass any parameters at the same time, then the simplest way to do this is with Python's anonymous function lambda . This can be accomplished like the below:

from tkinter import *

root = Tk()

def login(variable):
    print("Login triggered")

login_button = Button(root, text="Login", command=lambda: login("variable"))
login_button.pack()

root.mainloop()

I never used tkinter but I guess it's because you're calling the get method while your initialising the gui. ( name = input_name.get() ) Instead you should only call this when the user clicks the button, which is inside the login method.

from tkinter import *

root = Tk()

label_name = Label(root, text="Username", fg="black")
label_password = Label(root, text="Password", fg="black")
input_name = Entry(root)
input_password = Entry(root, show="*")

label_name.grid(row=0, column=0, sticky=E)
label_password.grid(row=1, column=0, sticky=E)

input_name.grid(row=0, column=1)
input_password.grid(row=1, column=1)

checkbox = Checkbutton(root, text="Remember me")
checkbox.grid(columnspan=2, sticky=W)

login_button = Button(root, text="Login")
login_button.grid(row=1, column=2)


login_success = Label(root, text="Welcome", fg="black")
login_fail = Label(root, text="login fail", fg="black")


def login(event):
    name = input_name.get()
    password = input_password.get()
    if name == password:
        for widget in root.winfo_children():
            widget.grid_forget()
        login_true = Label(root, text="Welcome", fg="black")
        login_true.grid()
    elif name != password:
        for widget in root.winfo_children():
            widget.grid_forget()
        login_false = Label(root, text="login fail", fg="black")
        login_false.grid()


login_button.bind("<Button-1>", login)

root.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM