-2

This is a simple GUI program to check whether the user entered correct username and password. The problem is that even when inputting the correct username and password i.e admin and secret, it still outputs 'Invalid Login'

This is the code :

from tkinter import *
import tkinter.messagebox as box

def dialog1():
    box.showinfo('info','Correct Login')
def dialog2():
    box.showinfo('info','Invalid Login')

window = Tk()
window.title('Countries Generation')

frame = Frame(window)

Label1 = Label(window,text = 'Username:')
Label1.pack(padx=15,pady= 5)

entry1 = Entry(window,bd =5)
entry1.pack(padx=15, pady=5)

username = entry1.get()

Label2 = Label(window,text = 'Password: ')
Label2.pack(padx = 15,pady=6)

entry2 = Entry(window, bd=5)
entry2.pack(padx = 15,pady=7)

password = entry2.get()

if (username == 'admin' and  password == 'secret'):
    btn = Button(frame, text = 'Check Login',command = dialog1)
else:
    btn = Button(frame, text ='Check Login', command = dialog2)

btn.pack(side = RIGHT , padx =5)
frame.pack(padx=100,pady = 19)
window.mainloop()
3
  • stackoverflow.com/questions/16525582/… && stackoverflow.com/questions/28156719/… there are tons of examples out there to show you how to do it. Commented Jan 10, 2017 at 12:12
  • Any and all event fired checks should be done through a function. Your if statement is run once the script starts, move the check to a function which is called once the button is pressed instead. Commented Jan 10, 2017 at 12:15
  • Thank you, you helped a lot Commented Jan 10, 2017 at 12:35

1 Answer 1

0

i think you should think about using a class instead of the raw code which you will be having a good control . Any way i just corrected your logic instead of checking it before button click get the values after button click and then run

after that your code looks something like this

from tkinter import *
import tkinter.messagebox as box

def dialog1():
    username=entry1.get()
    password = entry2.get()
    if (username == 'admin' and  password == 'secret'):
        box.showinfo('info','Correct Login')
    else:
        box.showinfo('info','Invalid Login')


window = Tk()
window.title('Countries Generation')

frame = Frame(window)

Label1 = Label(window,text = 'Username:')
Label1.pack(padx=15,pady= 5)

entry1 = Entry(window,bd =5)
entry1.pack(padx=15, pady=5)



Label2 = Label(window,text = 'Password: ')
Label2.pack(padx = 15,pady=6)

entry2 = Entry(window, bd=5)
entry2.pack(padx = 15,pady=7)




btn = Button(frame, text = 'Check Login',command = dialog1)


btn.pack(side = RIGHT , padx =5)
frame.pack(padx=100,pady = 19)
window.mainloop()

hope that help you to understand what wrong with the previous code

Sign up to request clarification or add additional context in comments.

1 Comment

I don't use a class because this is for school and we have't done classes yet. Thank you so much I finally understood what I did wrong, you've been great help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.