如何从组合框中启用多选值?

jkn*_*ely 5 python combobox tkinter

Python 3.4.3、Windows 10、Tkinter

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

有没有一种简单的方法可以从组合框的下拉列表中启用多项选择?

Bry*_*ley 7

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

如果您需要能够进行多项选择,您可以使用带有关联菜单的菜单按钮,并向菜单添加复选按钮或单选按钮。

下面是一个例子:

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()
Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

10945 次

最近记录:

6 年,6 月 前