python scripts without gtk

Mark McLoughlin markmc at redhat.com
Fri Feb 18 00:12:33 PST 2005


On Thu, 2005-02-17 at 18:01 -0500, Andy Hanton wrote:
> I have written a small python script that listens for a usb key to be
> connected.  I want to run it from rc.local, but that fails because
> importing the gtk module makes the script try to connect to an X server.
> Is there a way to access the glib main loop from python without using
> the gtk module?

	Yeah, just use the gobject.MainLoop. The patch below is how you'd
change example-service.py.

Cheers,
Mark.

Index: example-service.py
===================================================================
RCS file: /cvs/dbus/dbus/python/examples/example-service.py,v
retrieving revision 1.6
diff -u -p -r1.6 example-service.py
--- example-service.py	11 Jul 2004 03:02:14 -0000	1.6
+++ example-service.py	18 Feb 2005 08:02:16 -0000
@@ -1,7 +1,6 @@
 import dbus
 
-import pygtk
-import gtk
+import gobject
 
 class SomeObject(dbus.Object):
     def __init__(self, service):
@@ -15,4 +14,5 @@ session_bus = dbus.SessionBus()
 service = dbus.Service("org.designfu.SampleService", bus=session_bus)
 object = SomeObject(service)
 
-gtk.main()
+main_loop = gobject.MainLoop ()
+main_loop.run ()




More information about the dbus mailing list