函数的If / else语句不起作用

时间:2017-10-16 13:35:19

标签: python tkinter

如果用户和密码相同,我点击登录按钮时会出现“登录成功/失败”文本。

我遇到的问题是无论我们写的是什么用户和密码,它总是显示“成功”消息而不是失败消息。

我使用.get来获取用户的输入并保存变量。

继承我的代码:

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()

编辑: 我也使用了“command”属性,但结果相同:

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: 修正了添加新条件

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()

2 个答案:

答案 0 :(得分:2)

它总是返回true的原因是因为你在两个本​​地命名空间中声明了namepassword。一个在代码的主体中,一个在函数login()中。 name函数中的passwordlogin()均等于None,因为它们从未具有指定的值。意味着它们始终相等并始终返回True

将功能更改为以下内容:

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()

.bind()的{​​{1}}小部件上使用Button往往是一个糟糕的主意。 <Button-1>小部件有一个名为Button的内置属性,可用于在单击时调用get call:

command

另外,如果您需要同时传递任何参数,那么最简单的方法就是使用Python的匿名函数from tkinter import * root = Tk() def login(): print("Login triggered") login_button = Button(root, text="Login", command=login) login_button.pack() root.mainloop() 。这可以像下面这样完成:

lambda

答案 1 :(得分:1)

我从未使用过tkinter,但我想这是因为你在初始化gui时调用了get方法。 (name = input_name.get()) 相反,您只应在用户单击login方法内的按钮时调用此方法。

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()