3

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

就像当用户单击选项框并单击 Apple 时,Tkinter 会返回一些东西

那么如果他将他的选择切换到,比如说,橙色,那么这也会在现场返回一些东西。

谢谢!


如何正确输入参数?

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()
4

2 回答 2

7

追踪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"))
于 2014-03-17T18:54:45.730 回答
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使下拉和检查按钮更容易以及解决其他烦恼的包。

于 2017-04-19T12:26:47.970 回答