使用pygtk在窗口中设置图像的位置 [英] Set position of image in a window using pygtk

查看:146
本文介绍了使用pygtk在窗口中设置图像的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用pygtk设置图像的位置?

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


推荐答案

GTK根据相对比对和填充而不是绝对像素位置来放置小部件。 gtk.Image的实例具有属性xalign,xpad,yalign,ypad,如果父级具有比所需空间更多的空间,则可用于定位小部件。

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.

例如

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

将图像置于窗口中心

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

会放置左上角的图像

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

会将图片放在左下角

如果你真的想处理固定位置,那么你需要使用gtk.Fixed小部件。通过方法 put(child,x,y)添加子项时,它允许指定显式位置。但请注意文档中的警告,这是一个坏主意,并会破坏用户界面。

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.

这篇关于使用pygtk在窗口中设置图像的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆