1

Is it possible to set the position of an image using pygtk?

import pygtk
import gtk

class Example:
    self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
    self.image = gtk.Image()
    self.image.set_from_file("example.png")
    # Position goes here (it should, shouldn't it?)
    self.window.add(self.image)
    self.image.show()
    self.window.fullscreen()
    self.window.show_all()
user446662
  • 13
  • 1
  • 3

1 Answers1

1

GTK lays widgets out based on relative alignments and padding, not absolute pixel positions. Instances of gtk.Image have properties xalign, xpad, yalign, ypad that can be used to position the widget if the parent has more space than is needed.

For example

self.image.xalign = 0.5
self.image.yalign = 0.5

would center the image in the window

self.image.xalign = 0
self.image.yalign = 0

would place the image in the upper left

self.image.xalign = 1
self.image.yalign = 1

would place the image in the bottom right

If you really want to deal with fixed positions then you need to use the gtk.Fixed widget. It allows to specify an explicit position when adding a child through the method put(child, x, y). Heed the warning in the documentation, though, that it's a bad idea and will make for a broken UI.

jcoppens
  • 5,306
  • 6
  • 27
  • 47
Geoff Reedy
  • 34,891
  • 3
  • 56
  • 79
  • I see. Let's say I want to center an image, and put an image about ~10 pixels above the centered image. Would it work with xalign, xpad, etc.? Would I have to do something similar to the following? self.image.xalign = 0.5 self.image.yalign = 0.55 Thank you for taking your time to help. – user446662 Sep 13 '10 at 19:26
  • @bakerblue, gtk.Window can have only one child widget. If you want to have more than one image, you'll need to put another container in the widget hierarchy. For example, gtk.VBox stacks its child widgets vertically. You should read the Packing Widgets section of the pygtk tutorial. – Geoff Reedy Sep 13 '10 at 19:46
  • Perhaps you should start using cairo. I use it to do what you are trying to do, i.e., lay one picture above other. For that you first create a source (the top image) and paste it over a surface (the already existing image). Other possibility is compose a "master image" with Python Image Library, and add this single image to your container. – heltonbiker Mar 27 '11 at 00:58