栏目分类:
子分类:
返回
文库吧用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
文库吧 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

OptionMenu小部件不过是一个便利类,它创建与菜单关联的菜单按钮。您可以通过

"menu"
属性进入此菜单。唯一的技巧是知道菜单项应该做什么,这无非就是设置关联变量的值。

这是一个例子:

import Tkinter as tkclass 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()


转载请注明:文章转载自 www.wk8.com.cn
本文地址:https://www.wk8.com.cn/it/640218.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 wk8.com.cn

ICP备案号:晋ICP备2021003244-6号