4

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.

Michael Gundlach
  • 106,555
  • 11
  • 37
  • 41
  • Does it really matter if the notification is in the center of screen? Why can't you let the window manager decide where it should go? – Zifre Apr 22 '09 at 19:04
  • i am working on a continuous testing setup for python, where every time i modify a file, the unit tests for my project are run, and a popup shows success or failure. it's pretty useless over in the bottom-right corner of the screen, because it pulls my eyes away from my work -- i might as well leave a console open. i want the notification to subconsciously inform me, like Growl on Mac would. – Michael Gundlach Apr 22 '09 at 19:32

3 Answers3

4

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
  • 803
  • 1
  • 8
  • 11
  • 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 Apr 23 '09 at 14:46
  • The periods after the "2"s are to make them floating point numbers - to avoid doing integer division which could cause problems. – Adam Apr 23 '09 at 22:02
0

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]
moinudin
  • 134,091
  • 45
  • 190
  • 216
-4

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
  • 1,405
  • 12
  • 19