Vie*_*urs 5 python dbus application-development
我在这里看到了类似的问题,但我没有得到答案。也许是因为我对这一切都很陌生,只是不明白。我希望我的应用程序主要用作指标。如果用户再次启动它,它会检查它是否已经在运行,如果它已经运行,则将所有输入数据提供给该进程并退出。
我正在尝试使用 Quickly 创建它。所以如果你能给我一些 python 示例或链接到类似的东西,那就太好了。
我发现我确实需要 DBus 来实现我所需要的。所以这就是我实际需要做的:
# using quickly...
#
# __init__.py
# # # # # # # # # # # # #
import dbus
import sys
from gi.repository import Gtk
# import whatever else you need...
from my_app import MyAppDBusService
# import whatever else from your app...
def main():
bus = dbus.SessionBus()
# Check if my app is running and providing DBus service
if bus.name_has_owner('com.example.myApp'):
#if it is running pass the commandline variables to it and exit
#get my service from DBus
myService = bus.get_object('com.example.myApp', '/com/example/myApp')
#get offered method from DBus
myMethod = myService.get_dbus_method('my_method', 'com.example.myApp')
#call the method and pass comandline varialbes
myMethod(sys.argv)
#exit
sys.exit(0)
else:
#if not running
#run my DBus service by creating MyAppDBusService instance
MyAppDBusService.MyAppDBusService()
#do whatever with sys.argv...
#...
Gtk.main()
# MyAppDBusService.py
# # # # # # # # # # # # # #
import dbus
import dbus.service
from dbus.mainloop.glib import DBusGMainLoop
#import whatever else you need...
# use the dbus mainloop
DBusGMainLoop(set_as_default = True)
class MyAppDBusService(dbus.service.Object):
def __init__(self):
# create dbus service in the SessionBus()
dbus_name = dbus.service.BusName('com.example.myApp', bus=dbus.SessionBus())
dbus.service.Object.__init__(self, dbus_name, '/com/example/myApp')
# offer a method to call using my dbus service
@dbus.service.method('com.example.myApp')
def my_method(self, argv):
#do whatever with argv...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
150 次 |
| 最近记录: |