有人可以解释Gtk2包装吗? [英] Can someone explain Gtk2 packing?

查看:100
本文介绍了有人可以解释Gtk2包装吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在项目中使用Gtk2.我将使用python/ruby​​.问题是包装对我来说似乎是个谜.我尝试使用VBox,以使我的窗口中可以包含以下小部件(按以下顺序):

  • 菜单栏
  • 工具栏
  • 文本视图/编辑器控件

我已经设法通过pack_start来猜测"自己的方式并获得了所需的布局,但是我希望能够理解它. Ruby Gtk2 上的文档似乎太不直观了(Python也是如此,因为它是相同的,仅针对python编写),您能否阐明一些想法?

此外,当我使用pack_start添加组件时,set_size_request并不总是有效.这是为什么 ?

解决方案

盒包装真的很简单,所以您可能不理解它是因为您认为它比实际要复杂./p>

布局可以是垂直的(如一堆砖)或水平的(如一排人).该布局中的每个元素都可以展开或不能展开.

水平(HBox)

[widget][widget][widget][widget]

垂直(VBox)

[widget]
[widget]
[widget]
[widget]

例如,一个带有两个按钮的水平布局(HBox),其代码为:

import gtk
box = gtk.HBox()
b1 = gtk.Button('button1')
b2 = gtk.Button('button2')
box.pack_start(b1)
box.pack_start(b2)

现在,由于打包的默认设置为expand=True,添加到框中的这两个按钮都将展开,并且每个按钮都将占据一半的面积.不管容器的大小是多少.我认为这是紧张".

扩展小部件:

[[    widget    ][    widget    ]]

因此,如果您希望其中一个按钮不展开,则可以像这样打包它:

box.pack_start(b1, expand=False)

非展开式小部件:

[[widget][        widget        ]]

然后,该按钮将仅占据其需要绘制的空间:文本+边框+阴影+图像(如果有)等.另一个按钮将展开以填充剩余的区域.通常,不需要扩展按钮,因此更现实的情况是您希望扩展以填充窗口的TextArea.

可以传递给pack_start的另一个参数是fill参数,通常可以忽略该参数.在这里可以说,如果expand=False,则完全忽略fill参数(因为在这种情况下它没有意义).

您提到的另一件事是set_size_request.我通常会说这不是一个好主意.我一般地说,因为在某些情况下您需要使用它.但是对于刚开始打包GTK +用户界面的用户,我强烈建议不要使用它.通常,让盒子和其他容器为您处理布局. set_size_request并没有完全按照您的期望执行.它不会更改窗口小部件的大小,而只会更改它将请求多少空间.它可能会使用更多,甚至可能会拉伸以填充更大的空间.它很少会变得比请求小,但又因为它只是一个请求",所以不能保证请求会得到满足.

I need to use Gtk2 for a project. I will be using python/ruby for it. The problem is that packing seems kind of mystical to me. I tried using a VBox so that I could have the following widgets in my window ( in the following order ):

  • menubar
  • toolbar
  • text view/editor control

I've managed to "guess" my way with pack_start and get the layout I need, but I'd like to be able to understand it. The documentation at Ruby Gtk2 seems way too unintuitive (and so is the python one, since it's the same, only written for python), could you shed some light?

Also, set_size_request isn't always working when I add a component with pack_start. Why is that ?

解决方案

Box packing is really simple, so perhaps your failure to understand it is because you imagine it is more complicated than it is.

Layout is either Vertical (like a pile of bricks) or horizontal (like a queue of people). Each element in that layout can expand or it can not expand.

Horizontal (HBox)

[widget][widget][widget][widget]

Vertical (VBox)

[widget]
[widget]
[widget]
[widget]

So for example, a Horizontal layout (HBox) with two buttons, which the code would be:

import gtk
box = gtk.HBox()
b1 = gtk.Button('button1')
b2 = gtk.Button('button2')
box.pack_start(b1)
box.pack_start(b2)

Now since the default for packing is to have expand=True, both those buttons added to the box will expand and they will each occupy half the area. No matter what the size of the container is. I think of this as "stretchy".

Expanding widgets:

[[    widget    ][    widget    ]]

So, if you want one of the buttons to not expand, you will pack it like this:

box.pack_start(b1, expand=False)

Non-expanding widget:

[[widget][        widget        ]]

Then the button will only occupy the space it needs to draw itself: text + borders + shadows + images (if any) etc. And the other button will expand to fill the remaining area. Normally, buttons don't need to be expanded, so a more real-life situation is a TextArea which you would want to expand to fill the window.

The other parameter that can be passed to pack_start is the fill parameter, and normally this can be ignored. It is enough here to say that if expand=False then the fill parameter is entirely ignored (because it doesn't make sense in that situation).

The other thing you mentioned is set_size_request. I would generally say that this is not a good idea. I say generally, because there are situations where you will need to use it. But for someone beginning out with packing a GTK+ user interface, I would strongly recommend not using it. In general, let the boxes and other containers handle your layout for you. The set_size_request does not do exactly what you would expect it to do. It does not change the size of a widget, but merely how much space it will request. It may use more, and may even stretch to fill larger spaces. It will rarely become smaller than the request, but again because it is just a "request" there is no guarantee theat the request will be fulfilled.

这篇关于有人可以解释Gtk2包装吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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