如何从gtkMenu中移除项目?

8 投票
2 回答
3291 浏览
提问于 2025-04-16 07:30

我用 gtk.Menu() 创建了一个菜单,并添加了一些菜单项,现在我想删除其中的一些菜单项。我该怎么做呢?

2 个回答

0

也许使用 destroy() 可以节省一些内存:

menu.foreach(lambda child: child.destroy())

9

这段代码应该能解决问题:

for i in menu.get_children():
    menu.remove(i) # check here if you want to remove this child

gtk.Menu 是从 gtk.Container 这个类继承过来的

你可以在这里查看相关文档

编辑

# First remove all old timer menu items from the gtkMenu
if TimerAppIndicator.menuList:
    for i in TimerAppIndicator.menuList:
        self.menu.remove(TimerAppIndicator.menuList[j])
        j+=1 <---- 1) j isn't declared here 
                   2) you will skip items why not just self.menu.remove(i)
                      you're already iterating over the menu items


# Delete all timer menu items from the list storing them
del TimerAppIndicator.menuList[:]
j=0  <--------- shouldn't this be before j+=1?

撰写回答