Question

I want to create a tkinter OptionMenu that edits another OptionMenu when it's changed. So I tried to create a command= argument which makes a specific command run on every update of the OptionMenu, like it does when I use the command= argument to a button, spinbox etc.

tl.wktype = OptionMenu(tl,wktypevar, *wk_types,command=typeupdate)

Somewhere else in the code is the typeupdate() Command - for debug purposes right now.

def typeupdate():
    typeval = tl.wktype.get()
    print(typeval)

The exception python throws is the following:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python33\lib\tkinter\__init__.py", line 1475, in __call__
    return self.func(*args)
  File "C:\Python33\lib\tkinter\__init__.py", line 3300, in __call__
    self.__callback(self.__value, *args)
TypeError: typeupdate() takes 0 positional arguments but 1 was given

What positional arguments does typeupdate() think is given and how do I fix this?

Was it helpful?

Solution

It is given the value that is clicked. To demonstrate, consider this script:

from tkinter import Tk, OptionMenu, StringVar
root = Tk()
def func(val):
    print(val)
var = StringVar()
OptionMenu(root, var, "one", command=func).grid()
root.mainloop()

When run (and when I click on the option "one" in the optionmenu), it prints "one" in the terminal.

So, in summary, add val (or any other argument name) to your function declaration and it will work:

def typeupdate(val):
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top