0

我想在 enaml 中创建一个多项目选择下拉菜单。

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

即使是能够密切复制能够从列表中选择多个项目的最终功能的东西,也会有所帮助,即使它不一定是下拉菜单。

4

1 回答 1

1

您可以使用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']
于 2018-03-13T20:49:07.197 回答