简体   繁体   中英

Ubuntu 12.04 application indicator python

How do i quit using a menu item in a python indicator application ?

menu = gtk.Menu()
buf = "Quit"
menu_items = gtk.MenuItem(buf) # click the Quit close the application. How to?
menu.append(menu_items)
menu_items.show()
ind.set_menu(menu)
gtk.main()

I would like to click the buf menu item to close the application. def quit:

This worked for me, import sys at top.

import sys

Then in the function that you want to quit put in:

sys.exit(0)

My working example below (check quitApplicaiton function):

#!/usr/bin/env python
#
#
# Authors: Stuart Page <xxx@gmail.com>
#
#

import gobject
import gtk
import appindicator
import sys

def menuitem_response(w, optionName):
    print optionName

def quitApplication(w, optionName):
    sys.exit(0)


if __name__ == "__main__":
    ind = appindicator.Indicator ("example-simple-client",
                                  "indicator-messages",
                                  appindicator.CATEGORY_APPLICATION_STATUS)

    ind.set_status (appindicator.STATUS_ACTIVE)
    ind.set_attention_icon ("indicator-messages-new")

    # create a menu
    menu = gtk.Menu()

    # create some drop down options

    optionName = "Menu Option - 1"
    menu_items = gtk.MenuItem(optionName)
    menu.append(menu_items)
    menu_items.connect("activate", menuitem_response, optionName)
    menu_items.show()

    optionName = "Menu Option - 2"
    menu_items = gtk.MenuItem(optionName)
    menu.append(menu_items)
    menu_items.connect("activate", menuitem_response, optionName)
    menu_items.show()

    optionName = "Quit"
    menu_items = gtk.MenuItem(optionName)
    menu.append(menu_items)
    menu_items.connect("activate", quitApplication, optionName)
    menu_items.show()



    ind.set_menu(menu)

    gtk.main()

There are several ways to implement this function

1st

import sys
def quit(widget):
    sys.exit(0)

2nd

import os
def quit(widget):
    os.kill(os.getpid(), 9)

3th

def quit(widget):
    gtk.main_quit()

then, you should connect your callback to menu item when click(activate) it, like following:

menu = gtk.Menu()
item = gtk.MenuItem("quit")
item.connect("activate", quit)
item.show()
menu.append(item)

while, i prefer the 3th method to define quit, It is the fastest as i test!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM