How to center a GNOME pop-up notification?

2.9k Views Asked by At

To display a GNOME pop-up notification at (200,400) on the screen (using Python):

import pynotify

n = pynotify.Notification("This is my title", "This is my description")
n.set_hint('x', 200)
n.set_hint('y', 400)
n.show()

I'm a gtk noob. How can I make this Notification show up centered on the screen, or at the bottom-center of the screen?

Perhaps my question should be "what Python snippet gets me the Linux screen dimensions?", and I'll plug those into set_hint() as appropriate.

3

There are 3 best solutions below

2
On BEST ANSWER

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()
3
On

on windows,

      from win32api import GetSystemMetrics
      width = GetSystemMetrics (0)
      height = GetSystemMetrics (1)
      print "Screen resolution = %dx%d" % (width, height)

I cant seem to find the linux version for it tho.

0
On

A bit of a hack, but this works:

from Tkinter import *
r = Tk()
r.withdraw()
width, height = r.winfo_screenwidth(), r.winfo_screenheight()

Another option is:

from commands import getstatusoutput
status, output = getstatusoutput("xwininfo -root")
width = re.compile(r"Width: (\d+)").findall(output)[0]
height = re.compile(r"Height: (\d+)").findall(output)[0]