如何从组合框中启用多个值选择? [英] How do I enable multiple selection of values from a combobox?

查看:101
本文介绍了如何从组合框中启用多个值选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python 3.4.3,Windows 10,Tkinter

Python 3.4.3, Windows 10, Tkinter

我正在尝试创建一个组合框,该组合框允许从下拉列表中进行多个选择。我发现列表框( Python Tkinter多项选择列表框)的工作类似,但无法获取它与组合框一起使用。

I am attempting to create a combobox that allows for multiple selections from the dropdown. I have found similar work for listbox (Python Tkinter multiple selection Listbox), but cannot get it work with the combobox.

是否有一种简单的方法可以从组合框的下拉菜单中进行多项选择?

Is there a simple way to enable multiple selection from the dropdown of the combobox?

推荐答案

通过设计ttk组合框不支持多个选择。它旨在允许您从选项​​列表中选择一项。

By design the ttk combobox doesn't support multiple selections. It is designed to allow you to pick one item from a list of choices.

如果需要能够进行多项选择,则可以使用带有相关菜单的menubutton ,然后在菜单上添加复选按钮或单选按钮。

If you need to be able to make multiple choices you can use a menubutton with an associated menu, and add checkbuttons or radiobuttons to the menu.

下面是一个示例:

import Tkinter as tk

class Example(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)

        menubutton = tk.Menubutton(self, text="Choose wisely", 
                                   indicatoron=True, borderwidth=1, relief="raised")
        menu = tk.Menu(menubutton, tearoff=False)
        menubutton.configure(menu=menu)
        menubutton.pack(padx=10, pady=10)

        self.choices = {}
        for choice in ("Iron Man", "Superman", "Batman"):
            self.choices[choice] = tk.IntVar(value=0)
            menu.add_checkbutton(label=choice, variable=self.choices[choice], 
                                 onvalue=1, offvalue=0, 
                                 command=self.printValues)
    def printValues(self):
        for name, var in self.choices.items():
            print "%s: %s" % (name, var.get())

if __name__ == "__main__":
    root = tk.Tk()
    Example(root).pack(fill="both", expand=True)
    root.mainloop()

这篇关于如何从组合框中启用多个值选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆