Set position of image in a window using pygtk

谁都会走 提交于 2019-12-11 07:38:13

问题


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()

回答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 left

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.



来源:https://stackoverflow.com/questions/3703509/set-position-of-image-in-a-window-using-pygtk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!