如何在开罗表面上绘制任何 GTK 小部件

发布于 2024-10-16 14:15:21 字数 153 浏览 4 评论 0原文

我想将 GTK 窗口的外观及其包含的所有按钮和其他小部件保存到 PNG 或 PDF 文件中。 Cairo 支持在此类表面上绘图。 我可以以某种方式要求 GTK 小部件在开罗表面上绘制自己吗? 代码示例将不胜感激,因为我是 GTK 和 Cairo 的新手。 Python 是我选择的语言。

I would like to save the looks of a GTK window, and all the buttons and other widgets it contains to a PNG or PDF file. Cairo supports drawing on such surfaces.
Could I somehow ask a GTK widget to draw itself on Cairo surface?
A code sample would be much appreciated, since I am a newcomer to both GTK and Cairo.
Python is my language of choice.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

木落 2024-10-23 14:15:21

In C, you can put your buttons and widgets in a GtkOffscreenWindow using gtk_widget_reparent() and then use gtk_offscreen_window_get_pixbuf() to render it onto a GdkPixbuf, which you can then save to a file. Sorry I don't have any Python code, but I don't think the offscreen window is available in PyGTK yet.

極樂鬼 2024-10-23 14:15:21

番茄说的话。如果您需要在屏幕外执行此操作,请使用 Gtk.OffscreenWindow,否则只需获取 Gdk 窗口和小部件的分配即可对其进行剪辑。这是一个可用于获取任何小部件的快照的片段。如以下代码所示,您还可以使用 Gtk.Widget.draw() 方法在 Cairo 上下文中渲染它。

from gi.repository import Gtk
import cairo

WINDOW_WIDTH, WINDOW_HEIGHT = 400, 300

window = Gtk.OffscreenWindow()
window.set_default_size(WINDOW_WIDTH, WINDOW_HEIGHT)
window.show()

canvas = Gtk.HBox()
window.add(canvas)
canvas.show()

button = Gtk.Button("Hello World!")
canvas.add(button)
button.show()

# this is needed, otherwise the screenshot is black:
while Gtk.events_pending():
    Gtk.main_iteration()

surf = cairo.ImageSurface(cairo.FORMAT_ARGB32,
                          WINDOW_WIDTH, WINDOW_HEIGHT)

cr = cairo.Context(surf)
canvas.draw(cr)
surf.write_to_png('test.png')

您可以临时将一个小部件(如 ptomato 所说)重新设置为此 Gtk.OffscreenWindow,以便制作快照。

original_parent = canvas.get_parent()
canvas.reparent(offscreen_window)

# do snapshot

canvas.reparent(original_parent)

What ptomato says. Use a Gtk.OffscreenWindow if you need to do it off screen, otherwise just get the Gdk window and the widget's allocation to clip it. Here is a snippet that can be used to get a snapshot of any widget. As the following code shows, you can also use the Gtk.Widget.draw() method to render it in the Cairo context.

from gi.repository import Gtk
import cairo

WINDOW_WIDTH, WINDOW_HEIGHT = 400, 300

window = Gtk.OffscreenWindow()
window.set_default_size(WINDOW_WIDTH, WINDOW_HEIGHT)
window.show()

canvas = Gtk.HBox()
window.add(canvas)
canvas.show()

button = Gtk.Button("Hello World!")
canvas.add(button)
button.show()

# this is needed, otherwise the screenshot is black:
while Gtk.events_pending():
    Gtk.main_iteration()

surf = cairo.ImageSurface(cairo.FORMAT_ARGB32,
                          WINDOW_WIDTH, WINDOW_HEIGHT)

cr = cairo.Context(surf)
canvas.draw(cr)
surf.write_to_png('test.png')

You can temporarily reparent a widget (as ptomato said) to this Gtk.OffscreenWindow, in order to make the snapshot.

original_parent = canvas.get_parent()
canvas.reparent(offscreen_window)

# do snapshot

canvas.reparent(original_parent)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文