1

I'm trying to trace a variable using a ComboBox Widget. When I change the ComboBox value I get the following error:

AttributeError: 'StringVar' object has no attribute '_report_exception'

What am I doing wrong?

import tkinter as tk
from tkinter import ttk, StringVar

class TEST(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        self.parent = parent

        self.estvalue = StringVar()

        self.pestanas = ttk.Notebook(self.parent)
        self.geometria = ttk.Frame(self.pestanas)
        self.viento = ttk.Frame(self.pestanas)
        self.topografia = ttk.Frame(self.pestanas)
        self.pestanas.add(self.geometria, text="Parámetros Globales")
        self.pestanas.add(self.viento, text="Viento")
        self.pestanas.grid(sticky=tk.W, pady=6, padx=6)
        Estructura = ["Edificios",
                      "Chimeneas, Tanques y Estructuras similares",
                      "Carteles llenos",
                      "Carteles abiertos y Estructuras similares"]
        self.Estructura = tk.LabelFrame(self.geometria, text="Estructura",
                                        labelanchor="nw", borderwidth="2",
                                        relief="groove", pady=5, padx=5)
        self.Estructura.grid(column=0, row=0, sticky=tk.W)
        self.Est = ttk.Label(self.Estructura, text="Tipo de Estructura")
        self.Est.grid(column=0, row=0, sticky=tk.W)
        self.Est = ttk.Label(self.Estructura, text="Tipo de Estructura")
        self.Est.grid(column=0, row=1, sticky=tk.W)
        self.boxest = ttk.Combobox(self.Estructura, textvariable=self.estvalue,
                                   state='readonly', width=36)
        self.boxest['values'] = Estructura
        self.boxest.current(0)
        self.boxest.grid(column=1, row=0, sticky=tk.E, padx=5)

        self.estvalue.trace_variable("w",self.eventest())

    def eventest(self):
        if self.estvalue.get() == "Edificios":
            print("foo")
        else:
            print("bar")

def main():
    root = tk.Tk()
    app = TEST(root)
    app.grid()
    root.title("App")
    root.focus_force()
    root.minsize(width=600, height=390)
    root.columnconfigure(0, weight=1)
    root.mainloop()

if __name__ == '__main__':
    main()
martineau
  • 119,623
  • 25
  • 170
  • 301
Eduardo
  • 687
  • 1
  • 6
  • 24

1 Answers1

3

You must give trace a reference to a function. Instead, you're calling the function and giving trace whatever the function returns.

Set up the trace like this (note the missing ())

self.estvalue.trace_variable("w",self.eventest)

The trace will pass along three values to the function, so you'll need to modify your function to accept those arguments even if you don't use them.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • That's not all. The definition of the handler has to be changed to accept three more arguments: `def eventest(self, a, b, c):`. I'm not sure what the additional arguments contain, but they can be ignored in this case. – martineau Dec 02 '16 at 20:22
  • @martineau: I addressed that in the last paragraph. As for what they are, see http://stackoverflow.com/questions/29690463/what-are-the-arguments-to-tkinter-variable-trace-method-callbacks – Bryan Oakley Dec 02 '16 at 20:25
  • Oops, sorry I missed that part of your answer. Regardless, thanks for the link. – martineau Dec 02 '16 at 20:27
  • Thanks you guys. The problem persists, but it is because of my lack of knowledge. – Eduardo Dec 02 '16 at 20:37
  • 1
    @Eduardo: When I made both the changes, the problem went away. What issue remains? – martineau Dec 02 '16 at 20:46