Amras Risyr February 2016

How to have an event check an entry to see if it matches a variable in Python

I'm wondering how to have Python check an event to see if username and password match, and then print correct if it is or false if it isn't. When I try the result is always false. I need a way to make it check to see if they match.

Here's my code:

    from tkinter import *

    root = Tk()

    def check(event):
    username = "Amras"
    password = "pass"
    if entry1 == username:
      if entry2 == password:
        print("true")
    else:
        print("false")
    else:
        print("false")

    name = Label(root, text="Name: ")
    password = Label(root, text="Password: ")
    entry1 = Entry(root)
    entry2 = Entry(root)
    c = Checkbutton(root, text="Keep me logged in")
    button1 = Button(root, text="Login")
    button1.bind("<Button-1>", check)

    name.grid(row=0, sticky=E)
    password.grid(row=1, sticky=E)

    entry1.grid(row=0, column=1)
    entry2.grid(row=1, column=1)

    c.grid(columnspan=2)

    button1.grid(row=1, column=2)

    root.mainloop()

Answers


Gábor ErdÅ‘s February 2016

You don't evaluate the text you put inside the entry box. Use entry1.get() etc...

from Tkinter import *

root = Tk()

def check(event):
    username = "Amras"
    password = "pass"
    # Also these if statements should be combined
    if entry1.get() == username:   # Use the .get() method
      if entry2.get() == password: # Use the .get() method
        print("true")
    else:
        print("false")


name = Label(root, text="Name: ")
password = Label(root, text="Password: ")
entry1 = Entry(root)
entry2 = Entry(root)
c = Checkbutton(root, text="Keep me logged in")
button1 = Button(root, text="Login")
button1.bind("<Button-1>", check)

name.grid(row=0, sticky=E)
password.grid(row=1, sticky=E)

entry1.grid(row=0, column=1)
entry2.grid(row=1, column=1)

c.grid(columnspan=2)

button1.grid(row=1, column=2)

root.mainloop()

Not after you type Amras and pass in you should get true

Also in your code the import statement seems bad (Tkinter with a capital by default), there are two else statements in the definition, and the indentation is wrong as well.

Post Status

Asked in February 2016
Viewed 2,490 times
Voted 12
Answered 1 times

Search




Leave an answer