TKinter OptionMenu: How to get the selected choice?

孤街醉人 提交于 2020-01-01 05:10:30

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/35132221/tkinter-optionmenu-how-to-get-the-selected-choice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!