1

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

MoMo
  • 13
  • 3
  • 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? – Bryan Oakley Sep 25 '22 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. – MoMo Sep 25 '22 at 22:55
  • You have to call `get` on an instance of `Entry`, not on the class (eg: `var.get()` in your example) – Bryan Oakley Sep 25 '22 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. – MoMo Sep 25 '22 at 23:13

2 Answers2

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.
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 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? – MoMo Sep 25 '22 at 23:14
  • 1
    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()`. – Tim Roberts Sep 25 '22 at 23:17
  • 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 Sep 25 '22 at 23:22
  • @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. – paxdiablo Sep 25 '22 at 23:22
  • @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. – paxdiablo Sep 25 '22 at 23:24
  • @paxdiablo, I modified the line to be varValue = var.get(). I then modified the button command to the following: 'buttonGo=Button(root, text="Generate", command= lambda: [print(f'Var is : {varValue}'), print(var.get())])'. Interestingly enough, when I input "blah" into the entry field I see the output: var is : blah. It seems like the second print statement actually gets me the value but the first doesn't. Evenwhen I assign it using varValue = var.get(). Any idea why that is? – MoMo Sep 25 '22 at 23:32
  • @MoMo, not without a better look at how those two code blocks interact and the exact changes you made. *Possibly* it's because `varValue` is not being set every time the entry is changed, rather once when it's created. So evaluating `varValue` gives the you same value over and over (probably empty), whereas `var.get()` *is* executed whenever the button is pressed and it get the value at that point. An easy way to find out would be to print out f"XXXXX '{varValue}'" at every point just after where you set `varValue`, this will tell you what actions cause it to happen. – paxdiablo Sep 26 '22 at 06:52
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.

MoMo
  • 13
  • 3