Python tkinter Entry Function not saving input into variable

Question:

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

Asked By: MoMo

||

Answers:

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.
Answered By: paxdiablo

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.

Answered By: MoMo
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.