本文介绍了TKinter OptionMenu:如何获得选定的选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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)

但我收到以下错误:

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)

我做错了什么?

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

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

可以给点建议吗?

推荐答案

OptionMenu 有一个内置的 command 选项,它给出了 的当前状态菜单 到一个功能.看到这个:

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.

这篇关于TKinter OptionMenu:如何获得选定的选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 16:32