代码之家  ›  专栏  ›  技术社区  ›  Robbert

使用Python在Gtk中将其他CheckMenuItems的活动状态设置为False

  •  2
  • Robbert  · 技术社区  · 7 年前

    def build_menu():
        menu = gtk.Menu()
    
        item_suspend = gtk.CheckMenuItem('Set lid to suspend')
        item_suspend.connect('activate', set_lid_suspend)
        menu.append(item_suspend)
    
        item_nothing = gtk.CheckMenuItem('Set lid to nothing')
        item_nothing.connect('activate', set_lid_nothing)
        menu.append(item_nothing)
    
        menu.show_all()
        return menu
    
    def set_lid_suspend(menuItem):
        call([os.path.join(__location__, "setLidSuspend.sh")])
        noot.update("<b>Set lid to suspend.</b>", "", None)
        noot.show()
    
        # adjusting the checkmarks
        menu = menuItem.get_parent()
        for item in menu.get_children(): # loop over all children
            if item.get_label() == 'Set lid to nothing':
                item.set_active(False)
                break
        menuItem.set_active(True)
    
    def set_lid_nothing(menuItem):
        call([os.path.join(__location__, "setLidNothing.sh")])
        noot.update("<b>Set lid to nothing.</b>", "", None)
        noot.show()
    
         # adjusting the checkmarks
        menu = menuItem.get_parent()
        for item in menu.get_children(): # loop over all children
            if item.get_label() == 'Set lid to suspend':
                print(item)
                item.set_active(False)
                break
        print("broke from nothing")
        menuItem.set_active(True)
    

    问题是,当我使用appIndicator并调用这两种方法中的一种时,一切都很好,并且表现良好;但当我选择另一种方法时,它们将在这两种方法之间永远交替循环。有人知道我做错了什么吗?

    此外,这是查找菜单项的正确方法吗?我发现很难相信没有这样的方法可以替代我使用的for循环。

    1 回复  |  直到 7 年前
        1
  •  1
  •   theGtknerd    7 年前

    我认为你使用了错误的方法。我会使用 RadioMenuItem 。然后,如果radiomenuitem的活动状态为False,我将阻止“更改”代码。这将导致如下结果:

    def build_menu():
        menu = gtk.Menu()
    
        item_suspend = gtk.RadioMenuItem('Set lid to suspend')
        item_suspend.connect('activate', set_lid_suspend)
        menu.append(item_suspend)
    
        item_nothing = gtk.RadioMenuItem('Set lid to nothing')
        item_nothing.connect('activate', set_lid_nothing)
        menu.append(item_nothing)
    
        item_suspend.join_group(item_nothing)
    
        menu.show_all()
        return menu
    
    def set_lid_suspend(menuItem):
        if menuItem.get_active() == False:
           return
        call([os.path.join(__location__, "setLidSuspend.sh")])
        noot.update("<b>Set lid to suspend.</b>", "", None)
        noot.show()
    
    
    def set_lid_nothing(menuItem):
        if menuItem.get_active() == False:
           return
        call([os.path.join(__location__, "setLidNothing.sh")])
        noot.update("<b>Set lid to nothing.</b>", "", None)
        noot.show()
    

    因为你发布了不完整的代码,所以我不能确定测试结果。试试看,让我知道。