-1

I would like to use the value entered in widget to define the parameter x, but it seems not working. I saw the question :Tkinter entry widget float but the answer seems not clear enough for me here is my code

from Tkinter import *

master = Tk()
Label(master, text="real value 1").grid(row=0)
Label(master, text="real value 2").grid(row=1)

e1=IntVar()
e2=IntVar()

e1 = Entry(master)
e2 = Entry(master)

e1.grid(row=0, column=1)
e2.grid(row=1, column=1)


mainloop( )

x=int(e1.get())

i get the error

invalid command name ".190173576L"
Sibo CHENG
  • 29
  • 3
  • For one thing, you have to specify the tkinter `Var`iables (`e1` and `e2` in your code) when creating each `Entry` widget by adding either a `textvariable=e1` or `textvariable=e2` keyword argument as appropriate. Here's some documentation on the [subject](http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/entry.html). – martineau Apr 09 '18 at 23:20
  • a) the `IntVar()`'s do nothing since you immediately overwrite them. b) you never even attempt to get the value of the `Entry()`'s, since you call `mainloop()`, which basically creates an infinite loop – Artemis Apr 10 '18 at 17:45

1 Answers1

0

Here is some example code to create and get the value from an Entry() widget:

from tkinter import *

master = Tk()
def display(val):
    print(val)
ent=Entry(master)
ent.pack()
btn=Button(master, text='Print Value', command=lambda: display(ent.get())).pack()

mainloop()
Artemis
  • 2,553
  • 7
  • 21
  • 36