如何在 linux 中捕获 Python 中的 gtk ApplicationWindow 图像?
How to capture image of a gtk ApplicationWindow in Python in linux?
我需要在 Python、linux 中捕获 ApplicationWindow 的图像。
这将是我假设的测试应用程序,取自 here:
from gi.repository import Gtk
import sys
class MyWindow(Gtk.ApplicationWindow):
# constructor for a Gtk.ApplicationWindow
def __init__(self, app):
Gtk.Window.__init__(self, title="Welcome to GNOME", application=app)
self.set_default_size(200, 100)
# create a label
label = Gtk.Label()
# set the text of the label
label.set_text("Hello GNOME!")
# add the label to the window
self.add(label)
class MyApplication(Gtk.Application):
def __init__(self):
Gtk.Application.__init__(self)
def do_activate(self):
win = MyWindow(self)
win.show_all()
def do_startup(self):
Gtk.Application.do_startup(self)
app = MyApplication()
exit_status = app.run(sys.argv)
sys.exit(exit_status)
我需要 window 的内容,不包括应用程序栏,保存在图像文件中。
on_save_image方法,将当前window保存为png文件。
您还可以在保存前对 cairo_context 应用 cairo 转换。
from gi.repository import Gtk, Gdk
import cairo
import sys
class MyWindow(Gtk.ApplicationWindow):
# constructor for a Gtk.ApplicationWindow
def __init__(self, app, image_file="/tmp/window.png"):
Gtk.Window.__init__(self, title="Welcome to GNOME", application=app)
self.set_default_size(200, 100)
self.image_file = image_file
vbox = Gtk.VBox()
self.add(vbox)
# create a label
label = Gtk.Label()
# set the text of the label
label.set_text("Hello GNOME!")
# add the label to the window
vbox.pack_start(label, True, True, 0)
button = Gtk.Button("ScreenshotMe!")
button.connect("clicked", self.on_save_image)
vbox.pack_start(button, False, False, 0)
def on_save_image(self, button):
"""
Get the surface of the current window and save it as a png
"""
window = self.get_window()
width, height = window.get_width(), window.get_height()
surface = Gdk.Window.create_similar_surface(window,
cairo.CONTENT_COLOR,
width, height)
cairo_context = cairo.Context(surface)
Gdk.cairo_set_source_window(cairo_context, window, 0, 0)
cairo_context.paint()
surface.write_to_png(self.image_file)
class MyApplication(Gtk.Application):
def __init__(self):
Gtk.Application.__init__(self)
def do_activate(self):
win = MyWindow(self)
win.show_all()
def do_startup(self):
Gtk.Application.do_startup(self)
app = MyApplication()
exit_status = app.run(sys.argv)
sys.exit(exit_status)
我需要在 Python、linux 中捕获 ApplicationWindow 的图像。
这将是我假设的测试应用程序,取自 here:
from gi.repository import Gtk
import sys
class MyWindow(Gtk.ApplicationWindow):
# constructor for a Gtk.ApplicationWindow
def __init__(self, app):
Gtk.Window.__init__(self, title="Welcome to GNOME", application=app)
self.set_default_size(200, 100)
# create a label
label = Gtk.Label()
# set the text of the label
label.set_text("Hello GNOME!")
# add the label to the window
self.add(label)
class MyApplication(Gtk.Application):
def __init__(self):
Gtk.Application.__init__(self)
def do_activate(self):
win = MyWindow(self)
win.show_all()
def do_startup(self):
Gtk.Application.do_startup(self)
app = MyApplication()
exit_status = app.run(sys.argv)
sys.exit(exit_status)
我需要 window 的内容,不包括应用程序栏,保存在图像文件中。
on_save_image方法,将当前window保存为png文件。
您还可以在保存前对 cairo_context 应用 cairo 转换。
from gi.repository import Gtk, Gdk
import cairo
import sys
class MyWindow(Gtk.ApplicationWindow):
# constructor for a Gtk.ApplicationWindow
def __init__(self, app, image_file="/tmp/window.png"):
Gtk.Window.__init__(self, title="Welcome to GNOME", application=app)
self.set_default_size(200, 100)
self.image_file = image_file
vbox = Gtk.VBox()
self.add(vbox)
# create a label
label = Gtk.Label()
# set the text of the label
label.set_text("Hello GNOME!")
# add the label to the window
vbox.pack_start(label, True, True, 0)
button = Gtk.Button("ScreenshotMe!")
button.connect("clicked", self.on_save_image)
vbox.pack_start(button, False, False, 0)
def on_save_image(self, button):
"""
Get the surface of the current window and save it as a png
"""
window = self.get_window()
width, height = window.get_width(), window.get_height()
surface = Gdk.Window.create_similar_surface(window,
cairo.CONTENT_COLOR,
width, height)
cairo_context = cairo.Context(surface)
Gdk.cairo_set_source_window(cairo_context, window, 0, 0)
cairo_context.paint()
surface.write_to_png(self.image_file)
class MyApplication(Gtk.Application):
def __init__(self):
Gtk.Application.__init__(self)
def do_activate(self):
win = MyWindow(self)
win.show_all()
def do_startup(self):
Gtk.Application.do_startup(self)
app = MyApplication()
exit_status = app.run(sys.argv)
sys.exit(exit_status)