Q:

Can't get tkinter's StringVar's trace method to work

Whenever I try to run my code, which looks a bit like this:
from tkinter import OptionMenu, StringVar, Tk

class Foo(OptionMenu):

    def __init__(self, master, options):
        self.bar = StringVar()
        self.bar.set(options[0])
        self.bar.trace("w", lambda: self.mouseCallback("foobar"))
        super().__init__(master, self.bar, *options)

    def mouseCallback(self, baz):
        print(baz)

def mainCycle():
    while True:
        root.update()

if __name__ == "__main__":
    opts = ["A", "LONG", "LIST", "OF", "OPTIONS"]
    root = Tk()
    foobarbaz = Foo(root, opts)
    foobarbaz.pack()
    mainCycle()

I get the following error:
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Program Files\Python35\lib\tkinter\__init__.py", line 1549, in __call__
    return self.func(*args)
  File "C:\Program Files\Python35\lib\tkinter\__init__.py", line 3285, in __call__
    self.__var.set(self.__value)
  File "C:\Program Files\Python35\lib\tkinter\__init__.py", line 260, in set
    return self._tk.globalsetvar(self._name, value)
_tkinter.TclError: can't set "PY_VAR0":

And even after a lot of "Stack Overflowing", I still can't get it to work. How can I avoid/fix this error?

A:

The signature of the callback function for StringVar.trace() should be something like def callback(*args), therefore the lambda you use in StringVar.trace() should be changed to:
self.bar.trace("w", lambda *args: self.mouseCallback("foobar"))

