You should define the function before you reference it.
>>> def f():
... convert_c2d(1)
... def convert_c2d(n):
... pass
...
>>> f()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in f
UnboundLocalError: local variable 'convert_c2d' referenced before assignment
>>> def g():
... def convert_c2d(n):
... pass
... convert_c2d(1)
...
>>> g()
>>>
Simply change of the codes as below will solve your problem:
def convert_c2d(n):
if not n.get():
return res.set("Insert a numeric value.")
try:
c = float(n.get())
diameter = c/pi
return res.set(str(diameter))
except ValueError:
return res.set("Insert a numeric value.")
convert = tk.Button(c2d, text="Convert", command=convert_c2d, activeforeground="red")
convert.grid(row=2, column=0, columnspan=2, pady=2, padx=2, sticky=E+W+N+S)
UPDATE
convert_c2d
takes a parameter. But tkinter does not call the command callback with argument. You should pass it using another function (or lambda as below):
convert = tk.Button(c2d, text="Convert", command=lambda: convert_c2d(entry_c),
activeforeground="red")
or you can change the convert_c2d
not to take any argument, and make the function directly access entry_c
. (nested function can access the variable from enclosing scope).