0

In Python tkinter, I want to get the value from an Entry widget that is in one function and use that value in another function, but I am unable to do so. Here is my code:

def login():
    print("This is your username: " + username_entry.get())
    print("This is your password: " + password_entry.get())

def login_screen()
    username_label = Label(root, text="Username", font=6)
    username_label.grid(row=0, column=0, pady=5, padx=80)

    username_entry = Entry(root, font=6)
    username_entry.grid(row=1, column=0, pady=5, padx=80)

    password_label = Label(root, text="Password", font=6)
    password_label.grid(row=2, column=0, pady=5, padx=80)

    password_entry = Entry(root, font=6)
    password_entry.grid(row=3, column=0, pady=5, padx=80)
    
    login_button = Button(root, text="Login",font=3, command=login)
    login_button.grid(row=7, column=0,  pady=(10,5), padx=80)
2
  • Welcome to Stack Overflow. Please read How to Ask and ask a question, keeping in mind that this is not a discussion forum. "I would really appreciate your help" is not a question but off-topic conversation that we aren't interested in, and "I am unable to do so" does not describe the problem. How exactly do you attempt to use the program? What happens when you do so? How is that different from what is supposed to happen? Commented Feb 3, 2022 at 11:25
  • Please also read stackoverflow.com/help/minimal-reproducible-example. We don't need your whole actual program, but we do need enough surrounding code to make a sensible program that is complete by itself. Commented Feb 3, 2022 at 11:26

2 Answers 2

1

If you mean that you just want the entries available to the login() function, you can pass them in as parameters:

def login(username_entry, password_entry):
    print("This is your username: " + username_entry.get())
    print("This is your password: " + password_entry.get())

def login_screen()
    username_label = Label(root, text="Username", font=6)
    username_label.grid(row=0, column=0, pady=5, padx=80)

    username_entry = Entry(root, font=6)
    username_entry.grid(row=1, column=0, pady=5, padx=80)

    password_label = Label(root, text="Password", font=6)
    password_label.grid(row=2, column=0, pady=5, padx=80)

    password_entry = Entry(root, font=6)
    password_entry.grid(row=3, column=0, pady=5, padx=80)

    login_button = Button(root, text="Login",font=3,
                          command=lambda: login(username_entry, password_entry))
    login_button.grid(row=7, column=0,  pady=(10,5), padx=80)

Here I've used a lambda to defer the execution of the login function.

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

Comments

0

You should return values that you want to use in other place. or use a class, or a global variable.

1 Comment

return them from what? When?

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.