Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7912811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:41:53+00:00 2026-06-03T13:41:53+00:00

I have a small application that uses a DrawingArea to draw a simple map

  • 0

I have a small application that uses a DrawingArea to draw a simple map using PyGObject and GTK3.

I load a Pixbuf using

from gi.repository import Gtk, GdkPixbuf
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size("logo.png", 25, 25)

and then try to draw it in the DrawingArea‘s draw event signal

def draw(self, widget, context):
    window = widget.get_window()
    ctx = window.cairo_create()
    ctx.set_source_pixbuf(pixbuf, 0, 0)

but I get the error message

"AttributeError: 'cairo.Context' object has no attribute 'set_source_pixbuf'"

If I’m reading the Gtk2 to Gtk3 migration guide correctly, this should work.
What am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T13:41:54+00:00Added an answer on June 3, 2026 at 1:41 pm

    The new draw signal uses a callback that already passes the cairo context as a parameter, you don’t need to do stuff like window = widget.get_window() like you did in PyGtk to get the cairo context while attending the expose-event signal. In PYGObject is simpler:

    import cairo
    
    class Foo(object):
        def __init__(self):
    
           (...)
            self.image = cairo.ImageSurface.create_from_png('logo.png')
           (...)
    
        def draw(self, widget, context):
            if self.image is not None:
                context.set_source_surface(self.image, 0.0, 0.0)
                context.paint()
            else:
                print('Invalid image')
            return False
    

    That is if you don’t need the PixBuf, but if you need it for something else you have several options:

    1. To have both objects in memory. If both are loaded from a PNG there should not be much problems other than the waste of memory.
    2. Convert GdkPixbuf to PIL Image, then PIL Image to data array and then create a Cairo ImageSurface from that data array using create_for_data(). Yak :S I don’t know better, sorry :S
    3. Use Gdk.cairo_set_source_pixbuf() proposed by hock. This seems to be the correct way to draw a Pixbuf in a ImageSurface, but it is totally unpythonic (and that’s why I hate this Introspection stuff, all looks like C, like a bad C port).

    If you choose the awful second option here is how:

    import Image
    import array
    from gi.repository import Gtk, GdkPixbuf
    
    width = 25
    height = 25
    pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size('logo.png', width, height)
    pil_image = Image.fromstring('RGBA', (width, height), pixbuf.get_pixels())
    byte_array = array.array('B', pil_image.tostring())
    cairo_surface = cairo.ImageSurface.create_for_data(byte_array, cairo.FORMAT_ARGB32, width, height, width * 4)
    

    Note that create_for_data() is not yet available for Python3, only for Python2.

    Check also my answer on how to use a double buffer in PyGObject if this is what you’re trying to achieve: Drawing in PyGobject (python3)

    Kind regards

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small application that uses a SharePoint list as the data source.
I have a small homework application that writes random numbers from 5 to 77
I have a small VB.NET application that I'm working on using the full version
I have a small application that uses the same API as the powerscript examples
I have an application that uses the bluetooth to receive some data (bytes) from
I have designed a small application in Python under Windows, that uses opencv. I
I have a small application that uses a navigation window and a set of
I'm using Eclipse to write a small Java application that uses WLST to manage
I have manage to create a small size js application that uses jQuery and
I have a web application that uses jQuery to load some sound effect on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.