Frage

I have this python code that takes screenshot of x screen.

#!/usr/bin/python
import gtk.gdk

w = gtk.gdk.get_default_root_window()
sz = w.get_size()
print "The size of the window is %d x %d" % sz
pb = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB,False,8,sz[0],sz[1])
pb = pb.get_from_drawable(w,w.get_colormap(),0,0,0,0,sz[0],sz[1])
if (pb != None):
    pb.save("screenshot.png","png")
    print "Screenshot saved to screenshot.png."
else:
    print "Unable to get the screenshot."

It works but i need to import from gi.repository import Gdk instead gtk.gdk. I have tried the following:

#!/usr/bin/python
from gi.repository import Gtk, Gdk, GdkPixbuf
from gi.repository.GdkPixbuf import Pixbuf

w = Gdk.get_default_root_window()
sz = w.get_size()
print "The size of the window is %d x %d" % sz
pb = Pixbuf(Gdk.COLORSPACE_RGB,False,8,sz[0],sz[1])
pb = pb.get_from_drawable(w,w.get_colormap(),0,0,0,0,sz[0],sz[1])
if (pb != None):
    pb.save("screenshot.png","png")
    print "Screenshot saved to screenshot.png."
else:
    print "Unable to get the screenshot."

But:

Traceback (most recent call last):
  File "./dsd", line 6, in <module>
    sz = w.get_size()
AttributeError: 'gtk.gdk.X11Window' object has no attribute 'get_size'

Is there a way to use completely gi.repository for this script? Thanks

EDIT: I solved the problem of AttributeError: 'gtk.gdk.X11Window' object has no attribute 'get_size' using sz = w.get_geometry() instead get_size. New code:

#!/usr/bin/python
from gi.repository import Gtk, Gdk, GdkPixbuf 
from gi.repository.GdkPixbuf import Pixbuf

w = Gdk.get_default_root_window()
sz = w.get_geometry()
#print "The size of the window is %d x %d" % sz
pb = GdkPixbuf.Pixbuf.new(GdkPixbuf.Colorspace.RGB,False,8,sz[2],sz[3])
pb = pb.get_from_drawable(w,w.get_colormap(),0,0,0,0,sz[2],sz[3])
if (pb != None):
    pb.save("screenshot.png","png")
    print "Screenshot saved to screenshot.png."
else:
    print "Unable to get the screenshot."

But also having another problem with this code:

Traceback (most recent call last):
  File "./dsd", line 9, in <module>
    pb = pb.get_from_drawable(w,w.get_colormap(),0,0,0,0,sz[2],sz[3])
AttributeError: 'Pixbuf' object has no attribute 'get_from_drawable'

How can i use "get_from_drawable" attribute of Pixbuf? Thanks again!

War es hilfreich?

Lösung

I know that this answer arrives a bit late, but better late than never.

Gdk3+ actually simplified the process of retrieving the Pixbuf from a GdkWindow. Thus, your code should be as follows:

#!/usr/bin/python
from gi.repository import Gdk, GdkPixbuf

w = Gdk.get_default_root_window()
sz = w.get_geometry()[2:4]
#print "The size of the window is %d x %d" % sz
pb = Gdk.pixbuf_get_from_window(w, 0, 0, sz[0], sz[1])
if (pb != None):
    pb.savev("screenshot.png","png", [], [])
    print "Screenshot saved to screenshot.png."
else:
    print "Unable to get the screenshot."

Andere Tipps

Perhaps you want get_width() and get_height().

According to their docs get_size() is deprecated: https://developer.gnome.org/gdk/stable/gdk-Drawing-Primitives.html#gdk-drawable-get-size

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top