如何根据下拉菜单中的选择动态填充 tkinter 中的选项小部件?

发布于 2024-12-04 14:14:12 字数 134 浏览 1 评论 0原文

我的问题如下:我有几个文件,并且我已经制作了一个包含名称的下拉菜单,接下来我需要的是一个选项菜单,只要选择文件名即可更改该菜单以显示特定文件中的一些数据,如下所示选项。需要明确的是,我的问题只是关于如何在选择下拉列表中的选项时更改选项菜单。感谢您的帮助。

my problem is the following: I have several files and I have made a drop down menu with the names,the next thing I need is an option menu which can be changed whenever a file name is selected to show some data from the specific file as option.To be clear,my question is only about how to make the option menu change when a choice from the drop down is selected.Thanks for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

计㈡愣 2024-12-11 14:14:12

OptionMenu 小部件只不过是一个方便的类,它创建与菜单关联的菜单按钮。您可以通过 "menu" 属性访问此菜单。唯一的技巧是知道菜单项应该做什么,无非是设置关联变量的值。

这是一个例子:

import Tkinter as tk

class SampleApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.om_variable = tk.StringVar(self)

        b1 = tk.Button(self, text="Colors", width=8, command=self.use_colors)
        b2 = tk.Button(self, text="Sizes", width=8, command=self.use_sizes)

        self.om = tk.OptionMenu(self, self.om_variable, ())
        self.om.configure(width=20)
        self.use_colors()

        b1.pack(side="left")
        b2.pack(side="left")
        self.om.pack(side="left", fill="x", expand=True)


    def _reset_option_menu(self, options, index=None):
        '''reset the values in the option menu

        if index is given, set the value of the menu to
        the option at the given index
        '''
        menu = self.om["menu"]
        menu.delete(0, "end")
        for string in options:
            menu.add_command(label=string, 
                             command=lambda value=string:
                                  self.om_variable.set(value))
        if index is not None:
            self.om_variable.set(options[index])

    def use_colors(self):
        '''Switch the option menu to display colors'''
        self._reset_option_menu(["red","orange","green","blue"], 0)

    def use_sizes(self):
        '''Switch the option menu to display sizes'''
        self._reset_option_menu(["x-small", "small", "medium", "large"], 0)

if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()

The OptionMenu widget is nothing more than a convenience class that creates a menubutton that is associated with a menu. You can get at this menu via the "menu" attribute. The only trick is to knowing what the menu items should do, which is nothing more than setting the value of the associated variable.

Here's an example:

import Tkinter as tk

class SampleApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.om_variable = tk.StringVar(self)

        b1 = tk.Button(self, text="Colors", width=8, command=self.use_colors)
        b2 = tk.Button(self, text="Sizes", width=8, command=self.use_sizes)

        self.om = tk.OptionMenu(self, self.om_variable, ())
        self.om.configure(width=20)
        self.use_colors()

        b1.pack(side="left")
        b2.pack(side="left")
        self.om.pack(side="left", fill="x", expand=True)


    def _reset_option_menu(self, options, index=None):
        '''reset the values in the option menu

        if index is given, set the value of the menu to
        the option at the given index
        '''
        menu = self.om["menu"]
        menu.delete(0, "end")
        for string in options:
            menu.add_command(label=string, 
                             command=lambda value=string:
                                  self.om_variable.set(value))
        if index is not None:
            self.om_variable.set(options[index])

    def use_colors(self):
        '''Switch the option menu to display colors'''
        self._reset_option_menu(["red","orange","green","blue"], 0)

    def use_sizes(self):
        '''Switch the option menu to display sizes'''
        self._reset_option_menu(["x-small", "small", "medium", "large"], 0)

if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()
把时间冻结 2024-12-11 14:14:12

由于 OptionMenu 提供了一个命令选项,我建议保留对已经给出的命令(如果有)的引用,并以这种方式将其传递给新选项:

参加,基于先前答案的变量

def __init__(self, *args, **kwargs):
    ...
    self.command=kwargs['command']
    ...

def _reset_option_menu(options, index=None):
    ...
    menu.add_command(label=string, 
                     command=lambda:self.command(),
                     value=self.om_variable.set(string))                         
        ...

希望它有任何用处
顺便说一句,布莱恩·奥克利给出的答案非常有用,

As the OptionMenu gives a command option, i would suggest to keep reference to the command already given (if any) and pass it to the new options this way:

attendion, variables based on the previous answer

def __init__(self, *args, **kwargs):
    ...
    self.command=kwargs['command']
    ...

def _reset_option_menu(options, index=None):
    ...
    menu.add_command(label=string, 
                     command=lambda:self.command(),
                     value=self.om_variable.set(string))                         
        ...

Hope its of any use
Btw, really usefull the answer given by Bryan Oakley,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文