我的目标是从窗口制作屏幕截图而不显示它。
代码:所有的Xvfb
东西都适用于 QT(示例),但似乎我不完全理解如何将它与 GTK 一起使用。
import gtk,webkit,gobject,sys,os,time
web=webkit.WebView()
url = 'http://google.com/'
web.open(url)
win=gtk.Window()
win.add(web)
silence_xvfb=True
display='1'
screen='0'
xvfb_timeout=3
pidfile = '/tmp/.X%s-lock' % display
redirect = '> /dev/null 2>&1'
if not silence_xvfb:
redirect = ''
cmd = ' '.join(['Xvfb', ':'+display, '-screen', screen, '1600x1200x24', redirect])
os.system(cmd+' &')
start = time.time()
while(True):
diff = time.time() - start
if(diff > xvfb_timeout):
raise SystemError("Timed-Out waiting for Xvfb to start - {0} sec".format(xvfb_timeout))
if(os.path.isfile(pidfile)):
break
else:
time.sleep(0.05)
os.putenv('DISPLAY', ':%s' % display)
def drawWindow(win):
width, height = win.get_size()
pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, width, height)
screenshot = pixbuf.get_from_drawable(win.window, win.get_colormap(),
0, 0, 0, 0, width, height)
screenshot.save('screenshot.png', 'png')
print 'screenshot saved'
win.show_all()
gobject.timeout_add(5000, drawWindow, win)
gtk.main()