Gtk-Python:是否可以打印窗口内容,该怎么做? [英] Gtk-Python : Is it possible to print a window content and how to do this?

查看:113
本文介绍了Gtk-Python:是否可以打印窗口内容,该怎么做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Gtk.Window,其中包含一个Gtk.Grid和Gtk.Labels. 我想打印此窗口上显示的所有内容.有可能这样做吗?

I have a Gtk.Window which contains a Gtk.Grid and Gtk.Labels. I would like to print all of what is displayed on this window. Is it possible to do this and how?

我已经搜索了教程,但只找到了一个可以打印pdf文件的文件.

I've searched for tutorials but only found one to print pdf files.

这是我的打印功能:

def on_print(self, widget):
    print("*** Print ***")
    print_op = Gtk.PrintOperation()
    print_op.set_n_pages(1)
    print_op.connect("draw_page", self.print_window)
    res = print_op.run(Gtk.PrintOperationAction.PRINT_DIALOG, None)

def print_window(self):

请问我应该在"print_window"回调函数中编写什么代码吗?

Could you tell me what should I code in the "print_window" callback function, please?

谢谢.

推荐答案

好,所以我修改了我在评论中链接的示例.这应该只抓住窗口内的框.

Ok, so I have reworked the example I linked in my comment. This should only grab the box inside the window.

from gi.repository import Gtk, Gdk
import cairo
import sys


class MyWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="Welcome to GNOME")
        self.set_default_size(200, 100)
        self.image_file = image_file="/home/house/window.png"
        self.vbox = Gtk.VBox()
        self.add(self.vbox)
        label = Gtk.Label()
        label.set_text("Hello GNOME!")
        self.vbox.pack_start(label, True, True, 0)
        self.button = Gtk.Button("ScreenshotMe!")
        self.button.connect("clicked", self.on_save_image)
        self.vbox.pack_start(self.button, False, False, 0)
        self.show_all()
        self.connect("destroy", Gtk.main_quit)

    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 = self.vbox.get_allocated_width(), \
                        self.vbox.get_allocated_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)

def main():
    app = MyWindow()
    Gtk.main()

if __name__ == "__main__":
    sys.exit(main())

这篇关于Gtk-Python:是否可以打印窗口内容,该怎么做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆