Im trying to get a text from a tkinter entry and put it inside a dictionary but I get PY_VAR(some number) instead

Question:

the code below is part of a class.

def expected_callback(self,expected_val_var,index,mode):
      dictionary[self.row][3] = expected_val_var
def dataType callback(self,event):
    expected_val_var = StringVar()
    expected_val_var.trace("w",self.expected_callback)
    expected_val = Entry(self.root, width=20, textvariable= expected_val_var)
    expected_val.insert(0,"Expected value")
    expected_val.grid(row=self.row,column=self.col+2)

Im trying to get a text from a tkinter entry and put it inside a dictionary but I get PY_VAR(some number) instead.

I also tried dictionary[self.row][3] = expected_val_var.widget.get() but it said that str has no get(). What can I do to get the users input from expected_val entry into the dictionary?

Asked By: evgeny

||

Answers:

not sure, shouldn’t it be

dictionary[self.row][3] = expected_val.get()

but I would rename

expected_val

to something like

entry_field

for to be clearer that it is the input
element which value is to be red

and are you sure about

deff

, I know it as

def

Answered By: ycom1

It is because the first argument of the callback for .trace() is the internal name (string) of the tkinter variable.

If you want to pass expected_val_var (instance of StringVar) to expected_callback(), use lambda on .trace() instead:

def expected_callback(self, expected_val_var):
    dictionary[self.row][3] = expected_val_var.get()

def dataType_callback(self, event):
    ...
    expected_val_var.trace("w", lambda *args: self.expected_callback(expected_val_var)
    ...
Answered By: acw1668
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.