0

I am trying to use tkinter to have a user input variables to pass it into a function. The problem is , the input from the user is not being assigned to the actual variable for whatever reason.

root = Tk()
root.geometry("600x300")


MainWindow = Label(root, text = "Input var:")
MainWindow.grid(row=0, column = 0)
var= Entry(root)
var.grid(row=0, column=1)

I have about 20 variables being asked for in the GUI that has similar code to the above.

I then assign a button to display what is assigned to the variable (for troubleshooting purposes, the original purpose of the button is to pass the variables into a function).

buttonGo=Button(root, text="Generate", command= lambda: print(f'Var is : {var}'))
buttonGo.grid(row=20, column=1)
buttonExit
buttonExit.grid(row=20, column=2)    

root.mainloop()

When I run the program and click on the "Generate" button, I get the below output and not what I define it in the program.

Var is : .!entry

4
  • There's a difference between a widget and the data managed by a widget. You're printing out the widget. Are you aware of the get method? Commented Sep 25, 2022 at 22:52
  • I've tried using the get method by modifying the var assignment line to: "var= Entry.get(root)". I end up getting an error message saying "bad option "get": must be cget or configure". Using cget or configure gives me different error messages. Commented Sep 25, 2022 at 22:55
  • You have to call get on an instance of Entry, not on the class (eg: var.get() in your example) Commented Sep 25, 2022 at 22:58
  • Do you mind explaining a bit more? Everywhere I look I see documentation explaining how the variable should equal to the instance of Entry. You're example is the first I'm seeing like this. Commented Sep 25, 2022 at 23:13

2 Answers 2

2

That .!entry is the widget, not the value. If you want the value, you need to use the get() method of the widget.

In other words, something like:

value = var.get()
# Now use value.
Sign up to request clarification or add additional context in comments.

7 Comments

I've tried using the get method by modifying the var assignment line to: "var= Entry.get(root)". I end up getting an error message saying "bad option "get": must be cget or configure". Using cget or configure gives me different error messages. What am I doing wrong?
No, no, no. You need var to be the widget: var = Entry(root). When you want the value stored in the widget, you'll use var.get().
I added a 'var.get()' line right below the 'var = Entry(root)' and I get the same output of 'Var is : .!entry'. Not sure what I'm missing. @paxdiablo, also replying to the comment you just posted.
@MoMo, that change tries to call the class get() method which does not exist. As Tim states, var = Entry(...) is correct but you need var.get() to get the value.
@MoMo, var.get() on its own will get the value and throw it away. You need to do something like value = var.get() then use value. Updated the code to make that clearer.
|
0

Based on the replies I got, I believe I found the answer to be using .get method when calling the function. I modified the line to:

buttonGo=Button(root, text="Generate", command= lambda: print(f'Var is : {var.get()}'))

Thanks for the replies and explanations.

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.