0

So I am trying to make a basic program that when someone enters data into an Entry box, it checks to see if it matches data in a text file. This is the code that I have but whenever I press the button to activate the function nothing happens, no error, nothing. I have been searching online and in stackoverflow all night long, please help. :(

    if login_form is 1:
        frame_2.destroy()
        label_2 = Label(frame_1, text="Username")
        label_3 = Label(frame_1, text="Password")
        label_1 = Label(frame_1, text="Name")
        svalue = StringVar()
        svalue_1 = StringVar()
        svalue_2 = StringVar()
        entry_1 = Entry(frame_1, textvariable=svalue)
        entry_2 = Entry(frame_1, textvariable=svalue_1)
        entry_3 = Entry(frame_1, textvariable=svalue_2)
        label_1.grid(row=0, sticky=E)
        label_2.grid(row=1, sticky=E)
        label_3.grid(row=2, sticky=E)
        entry_1.grid(row=0, column=1)
        entry_2.grid(row=1, column=1)
        entry_3.grid(row=2, column=1)

        def check():
            directory = r'C:\Users\Ethan\Desktop\PycharmProjects\Learning\UserList\%s' % svalue.get()
            os.chdir(directory)
            fr = open(svalue.get(), 'r')
            text = fr.readlines()
            username = svalue_1.get()
            password = svalue_2.get()
            if username is text[0] and password is text[1]:
                print("...")
            else:
                print("...")

        button_1 = Button(text="Login", fg="blue", command=check)
        button_1.grid(row=3, columnspan=2)
Ethan
  • 21
  • 1
  • 6
  • are your sure that login_form is 1? You don't have some other button showing that you are mistaking for button_1, do you? – jcfollower Jul 15 '15 at 19:59
  • This is only a chunk of the code, my main problem is the IF statement inside the function CHECK, for whatever reason SVALUE.GET() isn't working with the statement. And I don't know why. – Ethan Jul 15 '15 at 20:02
  • What happens if you add `print username` – jcfollower Jul 15 '15 at 20:09
  • I'm wondering if the problem is that svalue is defined outside of the check function – jcfollower Jul 15 '15 at 20:12
  • I need it to do something else, not print, and no I ran so many tests to make sure it wasn't anything like that. I tried using different loops and functions and they all worked. I need the IF statement to work but that is the only one that doesn't. – Ethan Jul 15 '15 at 20:33
  • You should use `==` instead of `is` – Eric Levieil Jul 15 '15 at 21:12

1 Answers1

1

The basic problem is that is does not do what you think it does. It doesn't verify that two strings are equal, it verifies that two variables point to the same object. The if statement in your code will never be true since the values you are trying to compare are different objects (read: two distinct local variables)

You have a further problem that the data you get from the file via readlines will have newlines in the data, but the data from the widget will not. You should strip off the newlines before doing the compare.

You need to change this:

if username is text[0] and password is text[1]

... to this:

if username == text[0].strip() and password == text[1].strip()

For more information about the is operator you can read the answers for the question Understanding Python's "is" operator

Community
  • 1
  • 1
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685