Skip to content
Advertisement

Trying to declare a lambda function inside command parameter for the button but it doesn’t work(Tkinter)

I’m trying to enter a function with parameters without calling it by using lambda but I bump into an error, do you know why?

Here’s the function:

accountName = ""
accountPassword = ""

def switchScreenState(newState,username,password):
    screenState = newState
    if username != None & password != None:
        accountName = username
        accountPassword = password

All I’m trying to do is to change the accountName and accountPassword values for my code.

Here’s where I’m trying to write it down:

hashtagB = tk.Button(mainWindow, text="Automation by Hashtags", bg="blue", width=20, command=lambda _: switchScreenState("hashtag selection",usernameEntry.get(),passwordEntry.get()))
hashtagB.place(relx=0.5, rely=0.40, anchor="center")

And here’s from where I retrieve the new values:

usernameEntry = tk.Entry(mainWindow, width=30)
usernameCanvas.place(relx=0.5, rely=0.20, anchor="center")

passwordEntry = tk.Entry(mainWindow, width=30)
passwordEntry.place(relx=0.5, rely=0.32, anchor="center")

Thanks a lot in advance :)

Advertisement

Answer

The reason you’re getting error (in future please post the traceback in the question too) is that tkinter’s button requires command to be function of no arguments. So drop the underscore in your code.

hashtagB = tk.Button(mainWindow, text="Automation by Hashtags", bg="blue", width=20, command=lambda: switchScreenState("hashtag selection", usernameEntry.get(), passwordEntry.get()))

Also note that it’s better to check for None the following way and use and when you don’t mean bit operations:

if username is not None and password is not None:
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement