Home > Software design >  Why is it showing "wrong user or password" even if i entered the right user and password?
Why is it showing "wrong user or password" even if i entered the right user and password?

Time:06-20

just started learning tkinter today and immediately thought of coding a login and password GUI that tells whether your user and password are right or wrong

("admin" for user, and "defaultpass" for password)

from tkinter import *

root = Tk()


rightUser="admin"
rightPassword="defaultpass"
u = Entry(root, bg="gray",borderwidth=7)
p = Entry(root, bg="gray",borderwidth=7)
usertext= Label(root,text="Input User",borderwidth=7)
userpass= Label(root,text="Input Password",borderwidth=7)



def loggingin():
 if u.get == rightUser and p.get == rightPassword:
    result = Label(root, text="Succesfully Logged In")
    result.pack()
 else:
    result = Label(root, text="Wrong User or Password")
    result.pack()

loginButton = Button(root, text="Login", fg="green", borderwidth=7, command=loggingin)






usertext.pack()
u.pack()
userpass.pack()
p.pack()
loginButton.pack()



mainloop()

Thank you in advance!!

CodePudding user response:

Replace get with get(). Updated code:

from tkinter import *

root = Tk()


rightUser="admin"
rightPassword="defaultpass"
u = Entry(root, bg="gray",borderwidth=7)
p = Entry(root, bg="gray",borderwidth=7)
usertext= Label(root,text="Input User",borderwidth=7)
userpass= Label(root,text="Input Password",borderwidth=7)



def loggingin():
    if u.get() == rightUser and p.get() == rightPassword:
        result = Label(root, text="Succesfully Logged In")
        result.pack()
    else:
        result = Label(root, text="Wrong User or Password")
        result.pack()

loginButton = Button(root, text="Login", fg="green", borderwidth=7, command=loggingin)






usertext.pack()
u.pack()
userpass.pack()
p.pack()
loginButton.pack()



mainloop()

CodePudding user response:

Using python 3.11.0b3. No need to add more widgets. You can only use one reusable widget in line 31.

from tkinter import *
 

tkWindow = Tk()
tkWindow.title('Tkinter Login Form - pythonexamples.org')

rightUser="admin"
rightPassword="defaultpass"

usernameLabel = Label(tkWindow, text="User Name").grid(row=0, column=0)

username = StringVar()
usernameEntry = Entry(tkWindow, textvariable=username)
usernameEntry.grid(row=0, column=1)  

passwordLabel = Label(tkWindow,text="Password").grid(row=1, column=0)

password = StringVar()
passwordEntry = Entry(tkWindow, textvariable=password, show='*')
passwordEntry.grid(row=1, column=1)

 
result: None
def loggingin() -> None:
    global result
    if usernameEntry.get() == rightUser and passwordEntry.get() == rightPassword:
        result = "Succesfully Logged In"       
    else:
        result = "Wrong User or Password"
       
    Label(tkWindow, text=result).grid(row=4, column=1)
      
loginButton = Button(tkWindow, text="Login", command=loggingin).grid(row=4, column=0)  

tkWindow.mainloop()
  • Related