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

2024-05-28 22:23:47 发布

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

我为Ubuntu写了一个app指示器(基本上是wifi和电池图标旁边的图标)来设置关闭盖子的动作。它工作得很好,但我想添加一个复选标记▼看看哪个状态是活动的。这可以使用CheckMenuItems而不是Gtk中的MenuItems来完成。问题是,在我的例子中,CheckMenuItems的活动状态是互斥的。即当一个激活时,另一个不激活。我列出了代码中最相关的部分。你知道吗

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循环。你知道吗


Tags: to方法getdefitemactivemenuchildren
1条回答
网友
1楼 · 发布于 2024-05-28 22:23:47

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

因为你发布了不完整的代码,我不能确定测试这个。试试看,让我知道。你知道吗

相关问题 更多 >

    热门问题