简体   繁体   中英

How to get pixels from Gtk.DrawingArea?

I'm writing a simple paint like application:

#!/usr/bin/env python3

from gi.repository import Gtk


class Application(Gtk.Window):

    def __init__(self):
        super().__init__(Gtk.WindowType.TOPLEVEL, title='paint')
        self.connect('destroy', self.__on_destroy)
        vbox = Gtk.VBox()
        drawing_area = Gtk.DrawingArea()
        drawing_area.conenct('draw', self.__on_draw)
        vbox.pack_start(drawing_area, True, True, 2)
        self.add(vbox)
        self.show_all()

    def __on_draw(self, widget, g):
        g.set_source_rgb(200, 0, 0)
        g.move_to(16, 0)
        g.line_to(16, 32)
        g.stroke()

    def save_to_file(self, filename):
        # How to get pixels from drawing_area?
        pass

    def __on_destroy(self, e):
        Gtk.main_quit()

app = Application()
Gtk.main()

Now I want to save user drawing. How can I access pixels inside Gtk.DrawingArea widget?

Quote from this following SO link

Gtk.DrawingArea derives from Gtk.Window .Hence you can use Gdk.pixbuf_get_from_window() to get the contents of the drawing area into a GdkPixbuf.Pixbuf and then use the GdkPixbuf.Pixbuf.savev() function to write the pixbuf as an image on disk.

You can follow the link for complete code.Also this link can also help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM