1

I am creating a login system, where a user enters a password and username, and on a button being pressed, it goes to the function to check the password.

from tkinter import *

def login():
    Usr = Tk()
    Usr.title("Login")
    Usr.geometry("200x150+860+400")
    Usr.configure(bg="grey22")
    usrBox = Label(Usr, text = "Username:", font=( "arial",12, "bold"), fg="white", bg="grey22").place(x=50, y=10)
    passbox = Label(Usr, text = "Password:", font=( "arial",12, "bold"), fg="white", bg="grey22").place(x=50, y=60)
    usrName = StringVar()
    usernameInput = ""
    passwordInput = ""
    PssWord = StringVar()
    usrName = Entry(Usr, textvariable= usernameInput, width=15, bg="lightgrey").place(x=50, y=37)
    PssWord = Entry(Usr, textvariable= passwordInput, width=15, bg="lightgrey").place(x=50, y=87)
    enter = Button(Usr, text = "login", width=11, height = 1, bg="lightgrey", activebackground="grey", font=("arial", 10, "bold"), command = checkPassword: action(usernameInput, passwordInput)).place(x=50, y=110)
    Usr.mainloop()
def checkPassword(usernameInput, passwordInput):
    print(usernameInput, passwordInput)

login()

The action returns invalid syntax

2
  • command = lambda :checkPassword(usernameInput, passwordInput) Commented Jan 6, 2021 at 14:12
  • Also, usrBox = Label(...).place(x=50, y=10) is not the widget but None. Also, the two "text variables" should be actual StringVar and not just plain strings. Commented Jan 6, 2021 at 14:15

2 Answers 2

1

There are multiple problems with your code:

  • You have to create a lambda calling the checkPassword function.
  • You create some StringVar, but then pass plain strings to the Entry fields and use them in the callback; those will not get updated with the actual values, use the StringVar instead.
  • If you do x = Widget(...).layout(...), then x is not the widget but None, which is the result of all the layout functions (pack, grid, place, etc.), this is not a problem here, though, as you do not use all those variables anyway

Fixed code (excerpt)

usernameInput = StringVar()
passwordInput = StringVar()
Entry(Usr, textvariable=usernameInput, ...).place(x=50, y=37)
Entry(Usr, textvariable=passwordInput, ...).place(x=50, y=87)
Button(Usr, text="login", ..., command=lambda: checkPassword(usernameInput, passwordInput)).place(x=50, y=110)

Then, in the checkPassword function, use StringVar.get() to get the actual values.

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

Comments

0

You can use:

usrName = Entry(Usr, bd=3)
usrName.place(x=75, y=35)
enter = Button(Usr, text="Send", width=15, height=2,
       command=lambda: checkPassword(usrName.get()))
enter.place(x=25, y=75)

Comments

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.