Question

I am a beginner of Tkinter and i am trying to understand how open a new window to compute some variables.

In this example a first window ("input") with a button appears. After press the "start" a new window is create where i can import a Circumference value to convert in a diameter pressing the button "convert".

when i press this buttom an error appears in the console:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1470, in __call__
    return self.func(*args)
  File "C:/Users/samsung/PycharmProjects/test_FirstDensity/openANDread.py", line 55, in convert
    convert = tk.Button(c2d, text="Convert", command=convert_c2d, activeforeground="red")
UnboundLocalError: local variable 'convert_c2d' referenced before assignment

I tried several solutions without resolving my problem.

my code is:

from __future__ import division
from Tkinter import *
from math import pi
import tkMessageBox
import Tkinter, Tkconstants, tkFileDialog
from numpy import nan
import Tkinter as tk




class MainWindow(Frame):
    def __init__(self):
        Frame.__init__(self)
        self.filename = None
        self.master.title("input")
        self.master.minsize(250, 150)
        self.grid(sticky=E+W+N+S)

        top=self.winfo_toplevel()
        top.rowconfigure(0, weight=1)
        top.columnconfigure(0, weight=1)

        for i in range(1): self.rowconfigure(i, weight=1)
        self.columnconfigure(1, weight=1)

        self.button0 = Button(self, text="start", command=self.convert, activeforeground="red")
        self.button0.grid(row=0, column=1, pady=2, padx=2, sticky=E+W+N+S)

    def convert(self):
        c2d = Tk()
        c2d.title("C2DBH converter")
        c2d.minsize(350, 150)
        c2d.grid()

        res = StringVar(c2d)
        res.set("---")


        for i in range(3): c2d.rowconfigure(i, weight=1)
        c2d.columnconfigure(1, weight=1)

        label_c = tk.Label(c2d, text='Circumference')
        label_c.grid(row=0, column=0, padx=2, sticky=E+W+N+S)

        entry_c = Entry(c2d)
        entry_c.grid(row=0, column=1, padx=2,sticky=E+W+N+S)

        label_d = tk.Label(c2d, text='Diameter')
        label_d.grid(row=1, column=0, padx=2, sticky=E+W+N+S)

        result = tk.Label(c2d, textvariable=res, fg="red")
        result.grid(row=1, column=1, pady=2, padx=2, sticky=E+W+N+S)

        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)

        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.")
Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top