Search code examples
pythontkintertkinter-entry

Python tkinter Entry Function not saving input into variable


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


Solution

  • 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.