[pygtk] Pixmap, DrawingArea, Viewport, Scrolledwindow

Nick Peters nick.peters at wcgwave.ca
Fri Aug 14 08:47:43 WST 2009


Hello,

I am just starting out writing my first app (the first that i have done
on my own with out tutorials). It's a program to view images (they are
all roughly 2000x2000 pixels) and eventually i would like to add a
drawing feature so the user can add bits to the image. However, the
first step is to get an image to load properly into a drawingarea inside
a viewport that is inside a scrolledwindow. As my code now sits, i can
select an image file (using a filechooser dialog) and have gotten it to
load into the drawingarea. However once the image is loaded, the scroll
bars on the scrolledwindow do not allow you to scroll around the image
in the drawingarea. You can resize the program window (as it starts out
small) and more of the image will show, but the scroll bars are still
greyed out.

Is there a method to "update" the scrolledwindow with the size etc of
the new image? should it be doing it automatically? i have the  H and V
policies  on the scrolledwindow set to  always (even though that just
means they will show all the time). In anycase, here's my code thus far,
i am using glade for interface design:

#!/usr/bin/env python

#import what we need.
import sys
try:
    import pygtk
    pygtk.require("2.0")
except:
    pass
try:
    import gtk
    import gtk.glade
except:
    sys.exit(1) #exit if we don't have the proper libraries.

pixmap = None


class RPG_Map_Manager:
    ###############################################################
    ## Purpose: This calls the stuff to load the glade files, and##
    ##          does all the stuff needed to load up the GUI etc.##
    ###############################################################
    def __init__(self):
        #initialization of stuff.
        #set the glade file:
        self.gladefile = "rpg_map_manager.glade"
        self.wTree = gtk.glade.XML(self.gladefile)
        self.DMView = self.wTree.get_widget("DMView")
        #Create our dictionary and connect everything up:
        dic = { "on_openMenu" : self.openMenu,
            "on_wdwRPGMapM_destroy" : gtk.main_quit,
            "on_quitMenu" : gtk.main_quit,
            "on_DMView_expose_event" : self.on_DMV_expose,
            "on_DMView_configure_event" : self.on_DMV_configure }

        self.DMView.set_events(gtk.gdk.EXPOSURE_MASK
            | gtk.gdk.LEAVE_NOTIFY_MASK
            | gtk.gdk.BUTTON_PRESS_MASK
            | gtk.gdk.POINTER_MOTION_MASK
            | gtk.gdk.POINTER_MOTION_HINT_MASK)
   
        self.wTree.signal_autoconnect(dic)

    def openMenu(self, widget):
        #open menu event.
        global mask
        global pixmap
        open_dialog = gtk.FileChooserDialog("Select Map", None,
gtk.FILE_CHOOSER_ACTION_OPEN, (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
gtk.STOCK_OPEN, gtk.RESPONSE_OK))
        open_dialog.set_default_response(gtk.RESPONSE_OK)   
       
        #File filters for the open dialog.
        filter = gtk.FileFilter()
        filter.set_name("Map Images")
        filter.add_mime_type("image/png")
        filter.add_mime_type("image/jpeg")
        filter.add_mime_type("image/gif")
        #add the file filter
        open_dialog.add_filter(filter)
        response = open_dialog.run()
        if response == gtk.RESPONSE_OK:
            print open_dialog.get_filename(), 'selected'
            #load the Image the user selected.
            rpg_map =
gtk.gdk.pixbuf_new_from_file(open_dialog.get_filename())
            pixmap, mask = rpg_map.render_pixmap_and_mask()
        elif response == gtk.RESPONSE_CANCEL:
            print 'Closed, no files selected'
        open_dialog.destroy()

    def on_DMV_configure(self, widget, event):
        x, y, width, height = widget.get_allocation()
        pixmap = gtk.gdk.Pixmap(widget.window, width, height)
        pixmap.draw_rectangle(widget.get_style().white_gc, True, 0, 0,
width, height)
        return True

    def on_DMV_expose(self, widget, event):
        x, y, width, height = event.area
       
widget.window.draw_drawable(widget.get_style().fg_gc[gtk.STATE_NORMAL],
pixmap, x, y, x, y, -1, -1)
        return False

if __name__ == "__main__":
    hwg = RPG_Map_Manager()
    gtk.main()


Anyways, am i going about loading the image the correct way? Is there
something i am missing when using the scrolledwindow? I have been
searching the net and the list archives for any hints, as well as been
reading the pygtk tutorial and the reference manual for the widgets i
talked about and have yet to find an answer. I have also been looking at
some of the examples out there on the net, including scriblesimple.py
and drawingarea.py (which are found in the tutorial). Anyways, feel free
to comment on my code, i'd rather have to re-write what i have and have
it right than continue down the wrong road.

Thanks in advance for the help

-Nick Peters


More information about the pygtk mailing list