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

2024-05-26 21:53:49 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

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


Tags: 项目功能列表部件enamlcomboboxobjectcombo
1条回答
网友
1楼 · 发布于 2024-05-26 21:53:49

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

enaml multi select menu

像这样:

#                                       
# 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']

相关问题 更多 >

    热门问题