Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i import gtk.gdk from gi.repository

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!

like image 330
mnrl Avatar asked Aug 10 '13 23:08

mnrl


People also ask

What is GI repository?

gi. repository is the Python module for PyGObject (which stands for Python GObject introspection) which holds Python bindings and support for the GTK+ 3 toolkit and for the GNOME apps.

What is GTK in Python?

GTK is a multi-platform toolkit for creating graphical user interfaces. It is created in C language. GTK has been designed from the ground up to support a range of languages, including Python, Ruby, and Perl. The GTK library is also called the GIMP Toolkit.


1 Answers

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."
like image 182
Ivan De Paz Centeno Avatar answered Sep 20 '22 20:09

Ivan De Paz Centeno