使用python进行截图

使用python进行截图

下面代码当我将其作为控制台应用程序运行时,它工作得很好

import gtk.gdk
import time

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])

ts = time.time()
filename = "screenshot"
filename += str(ts)
filename += ".png"

if (pb != None):
    pb.save(filename,"png")
    print "Screenshot saved to "+filename
else:
    print "Unable to get the screenshot." 

但是现在当我在 ubuntu 应用程序中使用它时(使用 quick 和 glade),它给了我错误消息

 WARNING **: Couldn't connect to accessibility bus: Failed to connect to socket /tmp/dbus-O1o56xxlHA: Connection refused
/usr/lib/python2.7/dist-packages/gobject/constants.py:24: Warning: g_boxed_type_register_static: assertion `g_type_from_name (name) == 0' failed
  import gobject._gobject
/usr/lib/python2.7/dist-packages/gtk-2.0/gtk/__init__.py:40: Warning: specified class size for type `PyGtkGenericCellRenderer' is smaller than the parent type's `GtkCellRenderer' class size
  from gtk import _gtk
/usr/lib/python2.7/dist-packages/gtk-2.0/gtk/__init__.py:40: Warning: g_type_get_qdata: assertion `node != NULL' failed
  from gtk import _gtk

我怎样才能改进此代码以使其在基于 GUI 的应用程序中运行

编辑 我做了什么来改变代码

from gi.repository import Gdk, GdkX11, Gtk

        w = Gdk.get_default_root_window()
        geo = w.get_geometry()
        pb = GdkPixbuf.Pixbuf.new(GdkPixbuf.Colorspace.RGB,False,8,geo[2],geo[3])
        pb = pb.gdk_pixbuf_get_from_window(w,0,0,geo[2],geo[3])       
        pixbuf = pb.get_from_drawable(w,w.get_colormap(),0,0,0,0,geo[2],geo[3])        
        pixbuf.save("xdfgxfdgdfxgfdx.png","png")
        print "The size of the window is %d x %d" % sz
        pix = GdkPixbuf.Pixbuf.gdk_pixbuf_get_from_window(root_window, 0, 0, 500, 500)
        pix = GdkPixbuf.gdk_pixbuf_get_from_window(window,0,0,500,500);


       ts = time.time()
       filename = "screenshot"
       filename += str(ts)
       filename += ".png"

       if (pix != None):
           pix.save(filename,"png")
           print "Screenshot saved to "+filename
       else:
           print "Unable to get the screenshot." 

但它仍然给我错误

(0, 0, 1366, 768)
<Pixbuf object at 0x2966410 (GdkPixbuf at 0x2e42d90)>
Traceback (most recent call last):
  File "/home/srs/projectob-team/projectob_team/SelectmemoDialog.py", line 63, in on_start_clicked
    pb = pb.gdk_pixbuf_get_from_window(w,0,0,geo[2],geo[3])       
AttributeError: 'Pixbuf' object has no attribute 'gdk_pixbuf_get_from_window'

答案1

使用 PyGobject(Quickly 使用的 Gtk 版本)截屏的正确方法是:

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.")

您发布的第一个版本适用于较旧的 PyGtk。因此它在控制台上工作,因为它只加载 PyGtk。在 Quickly 应用中,会加载 PyGobject,您无法同时加载两者。您在寻找 get_from_drawable() 时遇到困难,请参阅“GdkDrawable 已消失”部分:

https://web.archive.org/web/20140808005032/https://developer.gnome.org/gtk3/stable/ch24s02.html#id-1.6.3.4.7

相关内容