GNOME3 マシンに USB メモリを刺した時等に出る Nothfy も DBus なんだね。
Na zdraví PyGI! ? Martin Pitt
うーん、やっぱり DBus は敷居が高いぞ。
GDBusProxy
(susssasa{sv}i) というワケワカメな表記は %s みたいなバリアント型の型指定子のようだ、Nothfy を使う場合にはコレで固定みたい。
一番上みたく作れというなら発狂するけどたしかに下方は簡単。
ただ一番下の result は long だったので添字はいらなかった。
#!/usr/bin/env python
#-*- coding:utf-8 -*-
from gi.repository import Gio, GLib
d = Gio.bus_get_sync(Gio.BusType.SESSION, None)
notify = Gio.DBusProxy.new_sync(d, 0, None, 'org.freedesktop.Notifications',
'/org/freedesktop/Notifications', 'org.freedesktop.Notifications', None)
result = notify.Notify('(susssasa{sv}i)', 'test', 1, 'gtk-ok', 'Hello World!',
'Subtext', [], {}, 10000)
# result type is long
print result #[0]
ただ Notify を使いたいだけなら Libnotify がある。
多分上記をラッピングしているだけだと思うけど。
同じものを Libnotify で作ろうと思ったけど引数の 1 が何か解らない。
NotifyUrgency 列挙体くらいしか相当するものが無いけど違ったし。
実際数値を何に変えても動作するので無視してもよさげだが。
#!/usr/bin/env python
#-*- coding:utf-8 -*-
from gi.repository import Notify
Notify.init("test")
# new(summary, body, icon)
notify = Notify.Notification.new("Hello World", "Subtext", "gtk-ok")
notify.set_timeout(10000)
notify.show()
print notify.props.id
とりあえず同じものができた。
result は int だったけど notify.show() の戻り値は bool なので困った。
プロパティの id と一致するようだ、表示毎に繰り上がるだけだったりする。
他にアップデート通知のようにボタンを表示して処理したい場合がある。
これは add_action で簡単に作成できるようだ。
ただし notify.show() は表示したら即制御を戻すのでコマンドを抜けてしまう。
なのでボタンを押した後の処理を入れるにはメインループが必要。
#!/usr/bin/env python
#-*- coding:utf-8 -*-
from gi.repository import Notify, Gtk
def on_callback(notifiaction, action, data=None):
dlg = Gtk.MessageDialog(
None,
Gtk.DialogFlags.MODAL,
Gtk.MessageType.INFO,
Gtk.ButtonsType.OK,
action)
dlg.set_title("message")
dlg.run()
dlg.destroy()
# quit
Gtk.main_quit()
Notify.init("test")
notify = Notify.Notification.new("Hello World", "Subtext", "gtk-ok")
# Set Button
notify.add_action("Action Text", "Button Text", on_callback, None, None)
# Do not sink
notify.set_urgency(Notify.Urgency.CRITICAL)
notify.set_timeout(10000)
notify.show()
# main loop
Gtk.main()
action 引数で振り分けもできるみたい。
これなら簡単だし結構使いみちがありそうです。

