Zenot1c
Zenot1c

Reputation: 25

Python Pass Parameters using tkinter command button

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

Upvotes: 1

Views: 138

Answers (2)

dalgy94
dalgy94

Reputation: 24

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)

Upvotes: 0

tobias_k
tobias_k

Reputation: 82899

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.

Upvotes: 1

Related Questions