Can someone explain Gtk2 packing?

廉价感情. 提交于 2019-12-11 02:07:35

问题


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 ?


回答1:


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.



来源:https://stackoverflow.com/questions/668226/can-someone-explain-gtk2-packing

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