views:

642

answers:

3

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.

A: 

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.

Fusspawn
GNOME doesn't run on Windows. This answer is completely useless.
Zifre
Not for those that might want to do the same under windows ;)
Fusspawn
oh and http://cygnome.sourceforge.net/
Fusspawn
I would like to not get viruses in Windows do you know how to do that in python?
Lucas McCoy
+1  A: 

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]
marcog
+1  A: 

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()
Adam
Works great, although I think the periods after the "2"s are typos.While I'm at it, any idea how to get the width of the notification itself, so I can subtract half of that off of the center of the screen?
Michael Gundlach
The periods after the "2"s are to make them floating point numbers - to avoid doing integer division which could cause problems.
Adam