4

I've spend past 3 days searching in google, how can I create a screenshot with PyGTK+3 ? There are gallizion tutorials about pyqt,pygtk+2,wx and PIL.

By the way, I don't need external programs like scrot, imlib2, imagemagick and so on.

HexSFd
  • 217
  • 4
  • 9
  • 1
    There is not such thing as PyGtk+3. I suppose you mean PyGObject. If you add a link to the PyGtk tutorial I can translate it for you to PyGObject, which is pretty straightforward must of the time. – Havok Dec 29 '13 at 05:06
  • The first answer: http://stackoverflow.com/questions/69645/take-a-screenshot-via-a-python-script-linux – HexSFd Dec 29 '13 at 07:43

2 Answers2

7

Since nobody else posted the translation to GTK3, here you go:

from gi.repository import Gdk

win = Gdk.get_default_root_window()
h = win.get_height()
w = win.get_width()

print ("The size of the window is %d x %d" % (w, h))

pb = Gdk.pixbuf_get_from_window(win, 0, 0, w, h)

if (pb != None):
    pb.savev("screenshot.png","png", (), ())
    print("Screenshot saved to screenshot.png.")
else:
    print("Unable to get the screenshot.")
gianmt
  • 1,162
  • 8
  • 15
3

Better later than never. I got stuck with get_from_drawable() but later found the documentation about its deprecation.

from gi.repository import Gdk

window = Gdk.get_default_root_window()
x, y, width, height = window.get_geometry()

print("The size of the root window is {} x {}".format(width, height))

# get_from_drawable() was deprecated. See:
# https://developer.gnome.org/gtk3/stable/ch24s02.html#id-1.6.3.4.7
pb = Gdk.pixbuf_get_from_window(window, x, y, width, height)

if pb:
    pb.savev("screenshot.png", "png", (), ())
    print("Screenshot saved to screenshot.png.")
else:
    print("Unable to get the screenshot.")
Havok
  • 5,776
  • 1
  • 35
  • 44
  • 1
    It's me HexSFd, stackoverflow blocked my previous account for asking "low quality questions" and wasn't able to respond to your post. Thank you Havok, wish you all the best. – SpringField Jan 12 '14 at 12:10
  • How can you modify this code so it takes a screenshot of the full screen? – Al Sweigart May 12 '22 at 15:14