如何创建多项目选择下拉列表?

How to create a Multi-item selection dropdown list?

我想在 enaml 中创建一个多项目 selection 下拉列表。

ComboBox 小部件提供此功能,但我们一次只能 select 一项。 ObjectCombo 小部件也是如此(但在功能上与 ComboBox 略有不同)。

即使某些东西与能够从列表中 select 多项的最终功能非常相似,也会有所帮助,即使它不一定是下拉列表。

您可以使用 PopupMenu 并使操作可检查。

像这样:

#------------------------------------------------------------------------------
# Copyright (c) 2018, Nucleic Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#------------------------------------------------------------------------------
""" This example demonstrates how to popup a menu.

A menu can be popped up in 2-ways. The first is by declaring the menu as
a child of a widget and setting the 'context_menu' attribute to True. The
second method is by creating the menu on-demand, and then invoking it's
'popup()' method to show the menu at the current mouse location.


"""
from __future__ import print_function
from enaml.widgets.api import (
    Window, Container, PushButton, Menu, Action, Field
)
from enaml.core.api import Looper

enamldef PopupMenu(Menu): menu:
    attr selected = set()
    attr choices = []
    Looper:
        iterable << choices
        Action:
            text = loop_item
            triggered :: print(text + ' triggered')
            checkable = True
            checked << self.text in selected
            checked ::  selected.add(self.text) if self.checked else selected.remove(self.text)


enamldef Main(Window):
    Container:
        PushButton:
            text = 'Popup Menu'
            attr selected = set()
            clicked :: PopupMenu(selected=self.selected,
                                 choices=['foo', 'bar', 'baz', 'bam']).popup()
        Field:
            text = 'Context Menu'
            read_only = True
            PopupMenu:
                context_menu = True
                choices = ['a', 'b', 'c', 'd']