0

I am trying to add new options to a Tkinter OptionMenu. I'm also watching for changes in the variables which the menu alters.

self.chosen_table = tk.StringVar(frame)
self.chosen_table.set("Please select a table...")
self.table_menu = tk.OptionMenu(frame, self.chosen_table,
                                "Please select a table...",
                                *self.tables.keys())
self.chosen_table.trace("w", self.update_current_table)

self.update_current_table is just an assignment, nothing fancy. I am trying to add options to the menu dynamically like so:

new_comm = tk._setit(self.chosen_table, table_name)
self.table_menu['menu'].add_command(label=table_name, command=new_comm)

This adds the new option to the menu just fine, but when I try to select the new option I get the following error:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/local/Cellar/python3/3.5.0/Frameworks/Python.framework/Versions/3.5/lib/python3.5/tkinter/__init__.py", line 1549, in __call__
    return self.func(*args)
  File "/usr/local/Cellar/python3/3.5.0/Frameworks/Python.framework/Versions/3.5/lib/python3.5/tkinter/__init__.py", line 3285, in __call__
    self.__var.set(self.__value)
  File "/usr/local/Cellar/python3/3.5.0/Frameworks/Python.framework/Versions/3.5/lib/python3.5/tkinter/__init__.py", line 260, in set
    return self._tk.globalsetvar(self._name, value)
_tkinter.TclError: can't set "PY_VAR0": 
Traceback (most recent call last):
  File "/usr/local/Cellar/python3/3.5.0/Frameworks/Python.framework/Versions/3.5/lib/python3.5/tkinter/__init__.py", line 1549, in __call__
    return self.func(*args)
TypeError: update_current_table() takes 1 positional argument but 4 were given

I don't know what arguments I'm giving to update_current_table, which only takes self as an argument, and I don't know how to check without altering a sizable chunk of Tkinter code. I'm certain that the problem is due to my adding an option dynamically, as I had no such errors before I did that.

Whonut
  • 288
  • 2
  • 12
  • 1
    `trace` send to `update_current_table` four arguments (with `self`) but your function expects only one (`self`). – furas Dec 15 '15 at 00:41

1 Answers1

1

trace send to update_current_table four arguments (with self) but your function expects only one - self. You probably need update_current_table(self, *args)

furas
  • 134,197
  • 12
  • 106
  • 148
  • Thanks. I couldn't find any documentation on what arguments the callback was being passed, which was my main point of confusion. I found answers at https://stackoverflow.com/questions/29690463/what-are-the-arguments-to-tkinter-variable-trace-method-callbacks. I also don't know why it only complains when I add options dynamically. Regardless, this seems to be the only way to remedy it. – Whonut Dec 15 '15 at 17:58