codeforfood
codeforfood

Reputation: 439

'var' is an invalid keyword argument for this function? (Tkinter and Python)

I am having trouble updating a label in tkinter. I looked at all the other questions i could find on this error but none were really relevant to my situation. Anyway, here is my code:

var = 100

v = StringVar()
v.set(str(var))
varLabel=Label(app, textvariable=v).grid(row=0)

#this is where i update my label
#also, this is where i get the error
v.set(str(var = var - dictionary[number]))

The error says:

'var' is an invalid keyword argument for this function

Any idea what I am doing wrong?

thanks

Upvotes: 1

Views: 1294

Answers (3)

James Polley
James Polley

Reputation: 8181

The error is here:

v.set(str(var = var - dictionary[number]))

I think you're expecting the interpreter to calculate var - dictionary[number]; assign that value into var; and then pass the value of var along to the str() function as the first argument.

The first part of that does actually work - the interpreter does calculate var - dictionary[number]. However, instead of putting that value into var, it passes that value along to the str function as an argument named var. Because the string function isn't expecting an argument named var you get the error you've seen.

Here's a quick iPython interpreter session showing this in action.

In [1]: def func1(var):
   ...:     print var
   ...:

In [2]: def func2(notvar):
   ...:     print notvar
   ...:

In [3]: var=12

In [4]: func1(var=var+3)
15

In [5]: func2(var=var+3)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-7e6ea7fc32e9> in <module>()
----> 1 func2(var=var+3)

TypeError: func2() got an unexpected keyword argument 'var'

In [6]:print var
12

You can see that func1, which does expect an argument named var, handles this fine. func2, which doesn't expect an argument called var, throws a TypeError about the unexpected keyword. The value of var is unchanged.

Upvotes: 1

Martijn Pieters
Martijn Pieters

Reputation: 1122172

The error indicates that the str() callable does not take a var keyword argument. The syntax you used is normally used for keyword arguments.

Assign separately:

var = var - dictionary[number]
v.set(str(var))

Upvotes: 3

karthikr
karthikr

Reputation: 99640

You are trying to do too many things at once.

Try this

var = var - dictionary[number]
v.set(str(var))

OR

var = str(var - dictionary[number])
v.set(var)

Upvotes: 4

Related Questions