1

问题1:

我试图制作一个脚本来与 Pidgins DBus 对话。我的脚本现在是这样的:

#!/usr/bin/env python

import dbus, gobject
from dbus.mainloop.glib import DBusGMainLoop

class DBus_Answer():
    def __init__(self, text):
        dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
        bus = dbus.SessionBus()
        self.answer = text

        bus.add_signal_receiver(self.my_func,
                                dbus_interface="im.pidgin.purple.PurpleInterface",
                                signal_name="ReceivedImMsg")
        loop = gobject.MainLoop()
        loop.run()

    def my_func(self, account, sender, message, conversation, flags):
        print sender, "said:", message
        bus = dbus.SessionBus()
        obj = bus.get_object("im.pidgin.purple.PurpleService", "/im/pidgin/purple/PurpleObject")
        purple = dbus.Interface(obj, "im.pidgin.purple.PurpleInterface")
        purple.PurpleConvImSend(purple.PurpleConvIm(conversation), self.answer)

run = DBus_Answer("My message!")

这工作正常。但我原来的程序正在使用PyQt4,我想用它QDBus来实现这一点。我进行了很多搜索,但没有找到有关此主题的任何有用文档。

问题2:我在某处读到python 3不支持DBus,是真的吗?它会用什么代替那个?

谢谢你们。

4

1 回答 1

1

我搜索了更多并找到了一些解决方案。现在我的代码是这样的并且工作得很好;-):

#!/usr/bin/env python

import sys
import dbus
from PyQt4.QtGui import QApplication
from dbus.mainloop.qt import DBusQtMainLoop

class DBus_Answer():
    def __init__(self, text):
        self.answer = text
        bus_loop = DBusQtMainLoop(set_as_default=True)
        self.bus = dbus.SessionBus()
        self.bus.add_signal_receiver(self.my_func,
                                     dbus_interface="im.pidgin.purple.PurpleInterface",
                                     signal_name="ReceivedImMsg")

    def my_func(self, account, sender, message, conversation, flags):
        obj = self.bus.get_object("im.pidgin.purple.PurpleService", "/im/pidgin/purple/PurpleObject")
        purple = dbus.Interface(obj, "im.pidgin.purple.PurpleInterface")
        purple.PurpleConvImSend(purple.PurpleConvIm(conversation), self.answer)

app = QApplication(sys.argv)
run = DBus_Answer("Slam")
app.exec_()
于 2012-09-12T20:39:54.160 回答