gowner
gowner

Reputation: 337

Exception raised with trace_variable()

I have a function that is supposed to build a window according to which option in a dropdown menu is selected:

def buildview():
    value = StringVar()
    options = ["one", "two", "three"]

    menu = OptionMenu(*(root, value) + tuple(options))

    ### Some window building accoring to the value selected... ###

    value.trace_variable("w", buildview)

The exception that is raised looks like this (EDIT: Entire Traceback):

Traceback (most recent call last):
  File "D:\Dropbox\PRO\infograbber\Infograbber 0.1.py", line 102, in <module>
    mainloop()
  File "C:\Python35\Python3564\lib\tkinter\__init__.py", line 405, in mainloop
    _default_root.tk.mainloop(n)
  File "C:\Python35\Python3564\lib\tkinter\__init__.py", line 1553, in __call__
    self.widget._report_exception()
AttributeError: 'StringVar' object has no attribute '_report_exception'

What exactly is causing this? Can I not have a method call back itself like this? I don't even know where to start fixing this problem, so I'd appreciate any help.

I'm using Python 3.5 64 bit, Sublime Text 2, Windows 10.

EDIT: Added a test callback function:

def test(*args):
        print("test")

and changed the above trace to

value.trace_variable("w", test)

Now the exception changed to this:

Traceback (most recent call last):
  File "C:\Python35\Python3564\lib\tkinter\__init__.py", line 1549, in __call__
    return self.func(*args)
  File "D:\Dropbox\PRO\infograbber\Infograbber 0.1.py", line 56, in buildview
    root.trace_variable("w", self.printcurrentarticle)
  File "C:\Python35\Python3564\lib\tkinter\__init__.py", line 1948, in __getattr__
    return getattr(self.tk, attr)
AttributeError: '_tkinter.tkapp' object has no attribute 'trace_variable'

Upvotes: 0

Views: 788

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 386255

I'm not entirely sure if this is the only problem, but it's definitely a problem. When a trace fires it passes in three arguments. The function you've defined takes no arguments. You need to pass in a reference to a function that takes at least three arguments.

You also have the problem that each time the trace fires, you are creating another variable and another trace. On the surface that seems like a bad idea, unless you really do want to create a new option menu every time an optionmenu changes.

Upvotes: 1

Related Questions