如何将复选框或单选按钮添加到 Unity 快捷列表?

如何将复选框或单选按钮添加到 Unity 快捷列表?

我一直在寻找如何实现这一点,但找不到任何地方。我甚至猜到了如何启用/禁用快速列表项,以及如何添加单击该项后调用的函数,但仅此而已。有什么想法吗?

我想为我的应用创建一个由复选框或单选按钮组成的快速列表。我找到了有关如何将没有相关操作的项目添加到快速列表的信息(教程)但这就是我找到的全部内容,没有关于如何添加其他类型的项目(复选框、单选按钮、水平分隔符或具有相关操作的项目)的信息。我试图得到类似

答案1

我不确定它是否正确,但我正在使用类似这样的东西:

  • 复选框:
def check_item_activated_callback (菜单项,a,b):
    如果 menuitem.property_get_int (Dbusmenu.MENUITEM_PROP_TOGGLE_STATE) == Dbusmenu.MENUITEM_TOGGLE_STATE_CHECKED:
       menuitem.property_set_int (Dbusmenu.MENUITEM_PROP_TOGGLE_STATE,Dbusmenu.MENUITEM_TOGGLE_STATE_UNCHECKED)
    别的:
       menuitem.property_set_int (Dbusmenu.MENUITEM_PROP_TOGGLE_STATE,Dbusmenu.MENUITEM_TOGGLE_STATE_CHECKED)

check1 = Dbusmenu.Menuitem.new()
check1.property_set (Dbusmenu.MENUITEM_PROP_LABEL, "复选框")
check1.property_set(Dbusmenu.MENUITEM_PROP_TOGGLE_TYPE,Dbusmenu.MENUITEM_TOGGLE_CHECK)
check1.property_set_int (Dbusmenu.MENUITEM_PROP_TOGGLE_STATE,Dbusmenu.MENUITEM_TOGGLE_STATE_CHECKED)
check1.property_set_bool (Dbusmenu.MENUITEM_PROP_VISIBLE,True)
check1.connect(Dbusmenu.MENUITEM_SIGNAL_ITEM_ACTIVATED,check_item_activated_callback,无)
qucklist.child_append (检查1)
  • 单选按钮:
def radio_item_activated_callback (radioitem1, a,radioitem2):
    radioitem1.property_set_int (Dbusmenu.MENUITEM_PROP_TOGGLE_STATE,Dbusmenu.MENUITEM_TOGGLE_STATE_CHECKED)
    radioitem2.property_set_int (Dbusmenu.MENUITEM_PROP_TOGGLE_STATE,Dbusmenu.MENUITEM_TOGGLE_STATE_UNCHECKED)

radio1 = Dbusmenu.Menuitem.new()
radio1.property_set (Dbusmenu.MENUITEM_PROP_LABEL, "单选按钮 1")
radio1.property_set (Dbusmenu.MENUITEM_PROP_TOGGLE_TYPE,Dbusmenu.MENUITEM_TOGGLE_RADIO)
radio1.property_set_int (Dbusmenu.MENUITEM_PROP_TOGGLE_STATE,Dbusmenu.MENUITEM_TOGGLE_STATE_UNCHECKED)
radio1.property_set_bool (Dbusmenu.MENUITEM_PROP_VISIBLE,True)
quicklist.child_append (radio1)

radio2 = Dbusmenu.Menuitem.new()
radio2.property_set (Dbusmenu.MENUITEM_PROP_LABEL, "单选按钮 2")
radio2.property_set (Dbusmenu.MENUITEM_PROP_TOGGLE_TYPE,Dbusmenu.MENUITEM_TOGGLE_RADIO)
radio2.property_set_int (Dbusmenu.MENUITEM_PROP_TOGGLE_STATE,Dbusmenu.MENUITEM_TOGGLE_STATE_CHECKED)
radio2.property_set_bool (Dbusmenu.MENUITEM_PROP_VISIBLE,True)
quicklist.child_append (radio2)

radio1.connect(Dbusmenu.MENUITEM_SIGNAL_ITEM_ACTIVATED,radio_item_activated_callback,radio2)
radio2.connect(Dbusmenu.MENUITEM_SIGNAL_ITEM_ACTIVATED,radio_item_activated_callback,radio1)
  • 分隔符(又称“水平分隔符”):
分隔符 = Dbusmenu.Menuitem.new();
分隔符.property_set(Dbusmenu.MENUITEM_PROP_TYPE,Dbusmenu.CLIENT_TYPES_SEPARATOR)
分隔符.property_set_bool (Dbusmenu.MENUITEM_PROP_VISIBLE,True)
quicklist.child_append (分隔符)
  • 启用/禁用菜单项:
item1 = Dbusmenu.Menuitem.new()
item1.property_set (Dbusmenu.MENUITEM_PROP_LABEL, "项目已启用")
item1.property_set_bool (Dbusmenu.MENUITEM_PROP_VISIBLE,True)
item1.property_set_bool (Dbusmenu.MENUITEM_PROP_ENABLED,True)
quicklist.child_append (项目1)

item2 = Dbusmenu.Menuitem.new()
item2.property_set (Dbusmenu.MENUITEM_PROP_LABEL, "项目已禁用")
item2.property_set_bool (Dbusmenu.MENUITEM_PROP_VISIBLE,True)
item2.property_set_bool (Dbusmenu.MENUITEM_PROP_ENABLED,False)
quicklist.child_append (项目2)

答案2

以下是制作复选框类型快速列表菜单项的示例:

    # Create toggle-able menu item for urgency
    urgent_menu_item = Dbusmenu.Menuitem.new ()

    # Set the tab's name as the menu item's name
    urgent_menu_item.property_set (Dbusmenu.MENUITEM_PROP_LABEL, _('Urgent'))

    # Make the menu item toggle-able
    urgent_menu_item.property_set(Dbusmenu.MENUITEM_PROP_TOGGLE_TYPE, Dbusmenu.MENUITEM_TOGGLE_CHECK)
    urgent_menu_item.property_set_int(Dbusmenu.MENUITEM_PROP_TOGGLE_STATE, Dbusmenu.MENUITEM_TOGGLE_STATE_UNCHECKED)
    urgent_menu_item.connect('item_activated', self.urgent_menu_item_activated)

    # Make the menu item visible
    urgent_menu_item.property_set_bool (Dbusmenu.MENUITEM_PROP_VISIBLE, True)

    # Add the section's menu item to the Quicklist menu
    quicklist.child_append(urgent_menu_item)

下面是制作 Radio 类型快捷列表菜单项的方法:

        # Create a new item for this section
        section_menu_item = Dbusmenu.Menuitem.new ()

        # Set the tab's name as the menu item's name
        section_menu_item.property_set (Dbusmenu.MENUITEM_PROP_LABEL, tab_name)

        # Make the menu item toggle-able
        section_menu_item.property_set(Dbusmenu.MENUITEM_PROP_TOGGLE_TYPE, Dbusmenu.MENUITEM_TOGGLE_RADIO)

        # Make the menu item visible
        section_menu_item.property_set_bool (Dbusmenu.MENUITEM_PROP_VISIBLE, True)

        # When the menu item is clicked, make it call menu_item_activated
        # with the tab id, which is used to make that the active tab
        section_menu_item.connect('item_activated', self.section_menu_item_activated, tab_id)

        # Add the section's menu item to the Quicklist menu
        quicklist.child_append(section_menu_item)

相关内容