python - 如何在 Linux 中用 Python 捕获 gtk ApplicationWindow 的图像?

标签 python linux gtk3

我需要在 Linux 中用 Python 捕获 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)

我需要将窗口的内容(应用程序栏除外)保存在图像文件中。

最佳答案

on_save_image 方法,将当前窗口保存为png文件。

您还可以在保存前将 cairo 转换应用于 cairo_context。

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 中用 Python 捕获 gtk ApplicationWindow 的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27880235/

相关文章:

python - Numpy NdArray 记忆化

python - Django asymettrical self 通过关系查询

python - 如何在 Pandas 中进行分组,在所有组上使用带有参数的函数并返回参数

php - 无法使用 PHP 运行 shell 脚本文件

linux - awk:使用不同的分隔符打印每个学生的平均值以及给定的详细信息

python - 如何从entrycompletion获取entry

widget - 远程控制/检查现有 GTK 应用程序

python - Psycopg2 中的元命令 -\d 不工作

linux - scp 文件没有设置正确的所有者

c - 以编程方式将数据添加到在 Glade 3.12.1 中创建的 GtkTreeView/GtkListStore