TKinter OptionMenu: How to get the selected choice?

Jackkilby picture Jackkilby · Feb 1, 2016 · Viewed 42.2k times · Source

I am quite new at Python and Tkinter, but I have to create a simple form which requires the use of drop-down menus. I was trying to do something like this:

#!/usr/bin python
import sys

from Tkinter import *

# My frame for form
class simpleform_ap(Tk):

    def __init__(self,parent):
        Tk.__init__(self,parent)
        self.parent = parent
        self.initialize()

    def initialize(self):
        self.grid()
        # Dropdown Menu
        optionList = ["Yes","No"]
        self.dropVar=StringVar()
        self.dropVar.set("Yes") # default choice
        self.dropMenu1 = OptionMenu(self, self.dropVar, *optionList)
        self.dropMenu1.grid(column=1,row=4)
        print self.dropVar.get()

def create_form(argv):
    form = simpleform_ap(None)
    form.title('My form')
    form.mainloop()

if __name__ == "__main__":
  create_form(sys.argv)

However what I get printed out is always the default value and I never get the value that I choose from the drop-down list.

I tried to use .trace method for the StingVar doing something like this:

#!/usr/bin python
import sys
from Tkinter import *

# My frame for form
class simpleform_ap(Tk):

    def __init__(self,parent):
        Tk.__init__(self,parent)
        self.parent = parent
        self.initialize()

    def initialize(self):
        self.grid()
        # Dropdown Menu
        optionList = ["Yes","No"]
        self.dropVar=StringVar()
        self.dropVar.set("Yes") # default choice
        self.dropMenu1 = OptionMenu(self, self.dropVar, *optionList)
        self.dropMenu1.grid(column=1,row=4)
        self.dropVar.trace("w",self.get_selection)
        print self.dropVar.get()


    def get_selection(self):
        print "Selected: "+ self.dropVar.get()

def create_form(argv):
    form = simpleform_ap(None)
    form.title('My form')
    form.mainloop()

if __name__ == "__main__":
    create_form(sys.argv)

But I got the following error:

Exception in Tkinter callback Traceback (most recent call last):
File "/usr/lib64/python2.6/lib-tk/Tkinter.py", line 1410, in __call__
    return self.func(*args) TypeError: get_selection() takes exactly 1 argument (4 given)

What am I doing wrong?

Please note, that I would prefer not to use any button to confirm the choice in the drop-down menu.

Could you please give some advice?

Answer

Gábor Erdős picture Gábor Erdős · Feb 1, 2016

The OptionMenu has a built in command option, which gives the current state of the menu to a function. See this:

#!/usr/bin python
import sys
from Tkinter import *

# My frame for form
class simpleform_ap(Tk):

    def __init__(self,parent):
        Tk.__init__(self,parent)
        self.parent = parent
        self.initialize()
        self.grid()

    def initialize(self):
        # Dropdown Menu
        optionList = ["Yes","No"]
        self.dropVar=StringVar()
        self.dropVar.set("Yes") # default choice
        self.dropMenu1 = OptionMenu(self, self.dropVar, *optionList,
                                    command=self.func)
        self.dropMenu1.grid(column=1,row=4)

    def func(self,value):
        print value


def create_form(argv):
    form = simpleform_ap(None)
    form.title('My form')
    form.mainloop()

if __name__ == "__main__":
    create_form(sys.argv)

This should do what you wish.