在选择Python之后立即选择optionmenu

时间:2014-03-17 18:39:59

标签: python-2.7 tkinter optionmenu

我想知道是否有任何方法可以让我看到用户在列表显示中选择了什么,让我们说:["Apple","Orange","Grapes"]在他们选择其中一个之后立即?

例如,当用户点击选项框并点击Apple时,Tkinter会返回一些内容

然后,如果他将他的选择切换到,那么就说,Orange,那么那也会当场返回。

谢谢!


如何正确输入参数?

from Tkinter import *

def option_changed(a):
    print "the user chose the value {}".format(variable.get())
    print a

master = Tk()

a = "Foo"
variable = StringVar(master)
variable.set("Apple") # default value
variable.trace("w", option_changed(a))

w = OptionMenu(master, variable, "Apple", "Orange", "Grapes")
w.pack()

mainloop()

2 个答案:

答案 0 :(得分:6)

追踪StringVar

from Tkinter import *

def option_changed(*args):
    print "the user chose the value {}".format(variable.get())
    print a

master = Tk()

a = "Foo"
variable = StringVar(master)
variable.set("Apple") # default value
variable.trace("w", option_changed)

w = OptionMenu(master, variable, "Apple", "Orange", "Grapes")
w.pack()

mainloop()

此处,只要用户从选项菜单中选择一个选项,就会调用option_changed


您可以将trace参数包装在lambda中以指定您自己的参数。

def option_changed(foo, bar, baz):
    #do stuff

#...
variable.trace("w", lambda *args: option_changed(qux, 23, "hello"))

答案 1 :(得分:6)

当我遇到烦人的界面小部件时 - 比如OptionMenu,我通常会在它周围编写一个类来抽象出恼人的属性。在这种情况下,我真的不喜欢每次想要创建下拉列表时使用StringVar的详细程度,所以我只创建了一个DropDown类,其中包含类中的StringVar(用Python 3.5编写,但很容易翻译成所有人):

class DropDown(tk.OptionMenu):
    """
    Classic drop down entry

    Example use:
        # create the dropdown and grid
        dd = DropDown(root, ['one', 'two', 'three'])
        dd.grid()

        # define a callback function that retrieves the currently selected option
        def callback():
            print(dd.get())

        # add the callback function to the dropdown
        dd.add_callback(callback)
    """
    def __init__(self, parent, options: list, initial_value: str=None):
        """
        Constructor for drop down entry

        :param parent: the tk parent frame
        :param options: a list containing the drop down options
        :param initial_value: the initial value of the dropdown
        """
        self.var = tk.StringVar(parent)
        self.var.set(initial_value if initial_value else options[0])

        self.option_menu = tk.OptionMenu.__init__(self, parent, self.var, *options)

        self.callback = None

    def add_callback(self, callback: callable):
        """
        Add a callback on change

        :param callback: callable function
        :return: 
        """
        def internal_callback(*args):
            callback()

        self.var.trace("w", internal_callback)

    def get(self):
        """
        Retrieve the value of the dropdown

        :return: 
        """
        return self.var.get()

    def set(self, value: str):
        """
        Set the value of the dropdown

        :param value: a string representing the
        :return: 
        """
        self.var.set(value)

使用示例:

# create the dropdown and grid, this is the ONLY required code
dd = DropDown(root, ['one', 'two', 'three'])
dd.grid()

# optionally, define a callback function that retrieves the currently selected option then add that callback to the dropdown
def callback():
    print(dd.get())

dd.add_callback(callback)

编辑添加:创建这篇文章后不久,我对tk的其他一些属性感到恼火,最后创建了一个名为tk_tools的软件包,使下拉和支票按钮更容易解决其他问题。