python - TKinter 选项菜单 : How to get the selected choice?

标签 python python-2.7 tkinter optionmenu

我是 Python 和 Tkinter 的新手,但我必须创建一个需要使用下拉菜单的简单表单。 我试图做这样的事情:

#!/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)

但是,我打印出来的始终是默认值,而我从未获得从下拉列表中选择的值。

我尝试使用 StingVar 的 .trace 方法来做这样的事情:

#!/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)

但是我得到了以下错误:

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)

我做错了什么?

请注意,我不希望使用任何按钮来确认下拉菜单中的选择。

你能给点建议吗?

最佳答案

OptionMenu 有一个内置的 command 选项,它将 menu 的当前状态提供给一个函数。看这个:

#!/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)

这应该如您所愿。

关于python - TKinter 选项菜单 : How to get the selected choice?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35132221/

相关文章:

python - 从 Python 字符串中删除零宽度空格 unicode 字符

Python TKinter 下拉菜单问题

python - 向文件添加行号

python - 从不同的选项分配值 - Pandas

python - 在使用 gensim 库进行训练时,Skip-gram word2vec 和 CBOW w2v 有什么区别?

python - devpi::AttributeError: 'FileUpload' 对象没有属性 'value'::上传时

接收和发出返回值的 Python 协程

python - 使用 SQLAlchemy 确定哪些列表值不在数据库列中

python - 如何确保 tkinter 条目的内容保存在 FocusOut 上?

python - 在这种情况下,如何将 apply(map, args) 转换为 map(function, iterable) ?