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

  • SEARCH
  • Home
  • 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 6615831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:30:38+00:00 2026-05-25T20:30:38+00:00

For testing and documentation purposes I would like to create a screenshot of a

  • 0

For testing and documentation purposes I would like to create a screenshot of a gtk.Window object. I’m following a basic pygtk sample at the moment. The example with my modifications looks like the following:

import gtk

def main():
    button = gtk.Button("Hello")
    scroll_win = gtk.ScrolledWindow()
    scroll_win.add(button)
    win = gtk.Window(gtk.WINDOW_TOPLEVEL)
    win.add(scroll_win)
    win.show_all()

    if win.is_drawable() is not True:
        raise RuntimeError("Not drawable?")

    width, height = win.get_size()
    pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8,
                            width, height)
    screenshot = pixbuf.get_from_drawable(win, win.get_colormap(),
                                          0, 0, 0, 0, width, height)
    screenshot.save('screenshot.png', 'png')

if __name__ == '__main__':
    main()
    gtk.main()

I’m ending up with an error when the get_from_drawable method is invoked.

TypeError: Gdk.Pixbuf.get_from_drawable() argument 1 must be gtk.gdk.Drawable, not gtk.Window

Apparently the screenshot example that I merged into the basic example is using a gtk.gdk.Window that inherits from gtk.gdk.Drawable.

My questions:

  • Is there another way to make the Pixbuf object capture the window?
  • Can I make a gtk.Window a gtk.gdk.Window or find the gtk.gdk.Window functionality within the gtk.Window?
  • 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-05-25T20:30:38+00:00Added an answer on May 25, 2026 at 8:30 pm

    The key difference to understand is that gtk.gdk.Window isn’t a “window” in the sense that most people think of. It’s not a GUI element, it’s just a section of the screen that acts as a logical display area, as explained in the documentation. In that way, it is more of a low-level object.

    A gtk.Window object (the traditional “window”) contains a window attribute which is the gtk.gdk.Window object that is used by the gtk.Window. It gets created when the window is initialized (in this case, after the call to win.show_all()).

    Here’s a revision of your code that saves the image correctly:

    import gtk
    import gobject
    
    def main():
        button = gtk.Button("Hello")
        scroll_win = gtk.ScrolledWindow()
        scroll_win.add(button)
        win = gtk.Window(gtk.WINDOW_TOPLEVEL)
        win.add(scroll_win)
        win.show_all()
    
        # Set timeout to allow time for the screen to be drawn
        # before saving the window image
        gobject.timeout_add(1000, drawWindow, win)
    
    def drawWindow(win):
        width, height = win.get_size()
        pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, width, height)
    
        # Retrieve the pixel data from the gdk.window attribute (win.window)
        # of the gtk.window object
        screenshot = pixbuf.get_from_drawable(win.window, win.get_colormap(), 
                                              0, 0, 0, 0, width, height)
    
        screenshot.save('screenshot.png', 'png')
    
        # Return False to stop the repeating interval
        return False
    
    if __name__ == '__main__':
        main()
        gtk.main()
    

    The timeout is necessary because even though the gtk.gdk.Window object has been created, the screen still hasn’t been drawn, until gtk.main() starts running, I think. If you read the pixel buffer right after win.show_all(), it will save an image, but the image will be the section of the screen that the window will later take up. If you want to try it for yourself, replace the call to gobject.timeout_add with a simple call to drawWindow(win).

    Note that this method saves an image of the GTK window, but not the border of the window (so, no title bar).

    Also, as a side point, when defining a function that was called using gobject.timeout_add, you don’t actually have to use return False explicitly, it just has to evaluate to a value equivalent to 0. Python return None by default if there is no return statement in the function, so the function will only be called once by default. If you want the function to be called again after another interval, return True.

    Using signals

    As noted by liberforce, instead of using a timeout, a better method would be to connect to the signals that are used by GTK to communicate events (and state changes in general).

    Anything that inherits from gobject.GObject (which all widgets do, basically) has a connect() function which is used to register a callback with a given signal. I tried out a few signals, and it appears that the one we want to use is expose-event, which occurs anytime a widget needs to be redrawn (including the first time it is drawn). Because we want the window to be drawn before our callback occurs, we’ll use the connect_after() variant.

    Here’s the revised code:

    import gtk
    
    # Don't use globals in a real application,
    # better to encapsulate everything in a class
    handlerId = 0
    
    def main():
        button = gtk.Button("Hello")
        scroll_win = gtk.ScrolledWindow()
        scroll_win.add(button)
        win = gtk.Window(gtk.WINDOW_TOPLEVEL)
        win.add(scroll_win)
    
        # Connect to expose signal to allow time
        # for the window to be drawn
        global handlerId 
        handlerId = win.connect_after('expose-event', drawWindow)
    
        win.show_all()
    
    def drawWindow(win, e):
        width, height = win.get_size()
        pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, width, height)
    
        # Retrieve the pixel data from the gdk.window attribute (win.window)
        # of the gtk.window object
        screenshot = pixbuf.get_from_drawable(win.window, win.get_colormap(), 
                                              0, 0, 0, 0, width, height)
        screenshot.save('screenshot.png', 'png')
    
        # Disconnect this handler so that it isn't
        # repeated when the screen needs to be redrawn again
        global handlerId
        win.disconnect(handlerId)
    
    if __name__ == '__main__':
        main()
        gtk.main()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For the purposes of unit testing, I would like to validate that two xml
I'm diving into unit testing and I would like to know if it is
Reading the documentation on Grails Unit testing I came across the following: In Grails
I'm following this guide about logic unit testing: https://developer.apple.com/library/ios/#documentation/DeveloperTools/Conceptual/UnitTesting/02-Setting_Up_Unit_Tests_in_a_Project/setting_up.html I've added tests to my
For testing purposes I have this shell script #!/bin/bash echo $$ find / >/dev/null
For testing purposes I have and SSL certificate set up on a dev site
There is good documentation out there on testing ActionMailer send methods which deliver mail.
While testing an ASP.NET application with perfmon, we find that the following field is
I've looked through the Google C++ Testing documentation, and whilst it makes reference to
Boost.Build documentation is quite laconic when it comes to testing. All tests in my

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.