Python program with Notification in Gnome Shell doesn't work
The GUI applications usually use three main components: widgets, event loop and callbacks. When you start that application, you create widgets, register callbacks and start event loop. Event loop is infinite loop which looks for events from widgets (such as 'clicked button') and fires corresponding callbacks.
Now, in your application you have another infinite loop, so these two will not play along. Instead, you should make use of the GLib.MainLoop().run()
to fire events. You can use GLib.timeout_add_seconds
to fire periodic events such as your every 10 seconds.
Second problem is that you need to hold reference to a notification which is supposed to call callbacks. The reason why it worked when you added GLib.MainLoop().run()
after noti.show()
is that reference to noti
still exists, but it would not work if you would do changes as I have suggested earlier. If you are sure there is always going to be just one notification active, you can hold the reference to the last notification. Otherwise you would need a list and periodically purge it or something along the lines.
The following example should set you in the right direction:
from gi.repository import GLib, Notify
class App():
def __init__(self):
self.last_notification = None
Notify.init('Test')
self.check()
def check(self):
self.last_notification = Notify.Notification.new('Test')
self.last_notification.add_action('clicked', 'Action',
self.notification_callback, None)
self.last_notification.show()
GLib.timeout_add_seconds(10, self.check)
def notification_callback(self, notification, action_name, data):
print(action_name)
app = App()
GLib.MainLoop().run()
action callback for libnotify not working
You need to integrate with D BUS to receive events from your notifications.
Here is good explanation use Glib for it.
Sending notifications with GObjects
Calling Notify.uninit
is not supposed to make the notifications disappear, it only tells libnotify that it will no longer be needed for your application. To make notifications disappear, you have to close them explicitly like in the following example:
import time
from gi.repository import Notify
Notify.init('myapp')
# optionally set an icon as the last argument
n = Notify.Notification.new('summary text', 'body text', "dialog-information")
n.show()
# do whatever your application is doing
time.sleep(10)
n.close()
Notify.uninit()
Missing image/icon in GNOME's desktop notification
Replace ~
in the string icon
with the full name of your home directory. Notify has trouble expanding that path by itself, so make it explicit to remove the problem.
Update ScrollView when height of a notification in the notification tray changes
I have figured it out. Changing the height of the notification changes it's visible height, not the one recognized by the parent's layout manager. Changing the parent's layout manager to class LabelExpanderLayout(), changing it's height on expand then updating the layout solves this problem.
How to center a GNOME pop-up notification?
Since you're using GNOME, here's the GTK way of getting the screen resolution
import gtk.gdk
import pynotify
n = pynotify.Notification("This is my title", "This is my description")
n.set_hint('x', gtk.gdk.screen_width()/2.)
n.set_hint('y', gtk.gdk.screen_height()/2.)
n.show()
How do I only clear one notification message using Notify and python?
I solved this by adding:
notification.set_hint('resident', GLib.Variant.new_boolean(True))
This sets the notification to only be cleared when the notification is clicked on.
Related Topics
How to Base-64 Encode a Png Image for Use in a Data-Uri in a CSS File
CSS Problems with Flask Web App
Groupby Weighted Average and Sum in Pandas Dataframe
How to Split a Huge CSV File Based on Content of First Column
Cron Job: How to Run a Script That Requires to Open Display
Fetching the Output of a Command Executed Through Os.System() Command
Pipe Raw Opencv Images to Ffmpeg
Get Local Network Interface Addresses Using Only Proc
How to Find the Owner of a File or Directory in Python
Change Working Directory in Shell with a Python Script
Getting Another Program's Output as Input on the Fly
Running Process of Remote Ssh Server in the Background Using Python Paramiko
Set the Hardware Clock in Python
Python:Undefined Symbol: Pyunicodeucs2_Decodeutf8
What Is the Naming Convention in Python For Variable and Function