【问题标题】:set other CheckMenuItems' active state to False in Gtk using Python使用 Python 在 Gtk 中将其他 CheckMenuItems 的活动状态设置为 False
【发布时间】:2018-01-14 00:09:43
【问题描述】:

我为 Ubuntu 编写了一个应用程序指示器(基本上是 wifi 和电池图标旁边的一个图标)来设置关闭盖子的动作。它工作得很好,但我想添加一个复选标记 ✓ 以查看哪个状态处于活动状态。这可以使用 CheckMenuItems 而不是 Gtk 中的 MenuItems 来完成。问题是,就我而言,CheckMenuItems 的活动状态是互斥的。 IE。当一个活跃时,另一个不活跃。我已经列出了代码中最相关的部分。

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 并调用这两种方法之一时,一切都很好,并且表现良好;但是当我选择另一种方法时,它们将永远在它们之间交替循环。有谁知道我做错了什么?

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

【问题讨论】:

    标签: python gtk menuitem appindicator


    【解决方案1】:

    我认为您使用了错误的方法。我会使用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()
    

    由于您发布了不完整的代码,我无法确定对此进行测试。试试看,让我知道。

    【讨论】:

    • 这很完美!非常感谢! RadioMenuItem 解决了整个相互排斥的事情。我真的不明白为什么你应该包含这些行来检查 menuItem 是否处于活动状态。我以为只有在点击相应的 menuItem 时才会调用这些方法?
    • 这取决于信号。 'clicked' 在取消选择时运行。 “激活”我不确定。无论如何,为了确定,我阻止了所有单选按钮 False 值。玩一玩,你就会明白它是如何工作的。
    • 顺便问一下,你叫什么noot?我自己制作了应用程序,并且总是寻找我不知道的功能:-)
    • noot = notify.Notification.new("","","") 在代码中更高 :) 这只是一个通知弹出窗口。
    猜你喜欢
    • 2013-08-31
    • 1970-01-01
    • 2020-08-30
    • 2019-10-01
    • 1970-01-01
    • 2018-05-29
    • 2014-10-11
    • 1970-01-01
    • 2020-11-16
    相关资源
    最近更新 更多