来自 gtk.Builder ui 字符串的指示器菜单

来自 gtk.Builder ui 字符串的指示器菜单

我创建了一个我认为应该可以工作的指示器,但实际上却不行。我从 glade 文件添加了 UI 界面,加载了它并将菜单添加到 appindicator。以下是从 XML 开始的完整源代码。这有什么理由不工作吗?

#!/usr/bin/env python
#coding: utf-8

import gtk
import sys
import appindicator

MENU_DEFINITION = """
<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <requires lib="gtk+" version="2.24"/>
  <!-- interface-naming-policy project-wide -->
  <object class="GtkMenu" id="jes_test_menu">
    <property name="visible">True</property>
    <property name="can_focus">False</property>
    <child>
      <object class="GtkCheckMenuItem" id="show_dialog">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="use_action_appearance">False</property>
        <property name="label" translatable="yes">Show dialog</property>
        <property name="use_underline">True</property>
        <signal name="toggled" handler="on_show_dialog_toggled" swapped="no"/>
      </object>
    </child>
    <child>
      <object class="GtkMenuItem" id="new_events">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="use_action_appearance">False</property>
        <property name="label" translatable="yes">New events</property>
        <property name="use_underline">True</property>
        <signal name="activate-item" handler="on_new_events_activate_item" swapped="no"/>
        <signal name="activate" handler="on_new_events_activate" swapped="no"/>
      </object>
    </child>
    <child>
      <object class="GtkMenuItem" id="exit_indicator">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="use_action_appearance">False</property>
        <property name="label" translatable="yes">Exit indicator</property>
        <property name="use_underline">True</property>
        <signal name="activate-item" handler="on_exit_indicator_activate_item" swapped="no"/>
        <signal name="activate" handler="on_exit_indicator_activate" swapped="no"/>
      </object>
    </child>
  </object>
</interface>
"""

class JesTestMenu:
    def __init__(self):

        ui = gtk.Builder()
        ui.add_from_string(MENU_DEFINITION)
        ui.connect_signals(self)

        menu = ui.get_object("jes_test_menu")

        ind = appindicator.Indicator("jes_test_menu", "indicator-messages", appindicator.CATEGORY_APPLICATION_STATUS)
        ind.set_status(appindicator.STATUS_ACTIVE)
        ind.set_attention_icon("new-messages-green")
        ind.set_menu(menu)
        menu.show_all()
        print("Indicator should now be visible")

    def on_exit_indicator_activate_item(self, widget, data=None):
        print("Exit activate item")
        sys.exit()

    def on_exit_indicator_activate(self, widget, data=None):
        print("Exit activate")
        sys.exit()

    def on_new_events_activate_item(self, widget, data=None):
        pass
    def on_new_events_activate(self, widget, data=None):
        pass
    def on_show_dialog_toggled(self, widget, data=None):
        pass

if __name__ == "__main__":
    menu = JesTestMenu()
    gtk.main()

答案1

问题是你的 Indicator 对象 ( ind) 不是类变量,它的作用域仅在函数中__init__。这意味着一旦你的类完成初始化,它就会被 Python 的垃圾收集器销毁。要解决这个问题,请将其替换indself.ind

self.ind = appindicator.Indicator("jes_test_menu", "indicator-messages", appindicator.CATEGORY_APPLICATION_STATUS)
self.ind.set_status(appindicator.STATUS_ACTIVE)
self.ind.set_attention_icon("new-messages-green")
self.ind.set_menu(menu)

相关内容