I have a Gtk.Box
with a vertical orientation for which i would like to have a static width. The reason is that there are children within the Box
with changing sizes, and i would like the whole layout to have the same look, instead of as it is now when the rest of the content in the window gets reordered
For example one of the Box
child widgets is a Label
which is given different strings during the running of the program. (I'm wrapping the contents so that the whole string is shown)
How can i set a static width for a Box container even though the container has children which want to expand the parent (container)?
What i've tried so far:
- Using
Widget.set_hexpand()
on the parent (layout) and children (widgets) - Using
Box.pack_start()
on the grand parent (which is an hbox) with the parent (layout) as the first argument and False for the expand and fill arguments
None of these works for me. It doesn't matter if the resize comes from an internal change in the program or if i resize the window, these still seem to have no effect
Any help is welcome!
EDIT:
Looking into this in more detail i have found that using pack_start
as described above has an effect only when the Label
has a text which is short enough that it does not collide with the widget edge
The behavior i can see when testing is that the text does get wrapped, but only after it has taken up as much space as it can, so it seems like the text length in a Label
has a precedence over pack_start
(and set_halign
) for determining the size of the Label
Using the code example below, switching out the short text for the long text to see the difference, and playing around with the size of the window it seems clear that this is how it works
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
win = Gtk.Window()
win.set_default_size(400, 300)
hbox = Gtk.Box(orientation = Gtk.Orientation.HORIZONTAL)
win.add(hbox)
vbox_left = Gtk.Box(orientation = Gtk.Orientation.VERTICAL)
vbox_left.set_hexpand(False) # <-----
vbox_right = Gtk.Box(orientation = Gtk.Orientation.VERTICAL)
long_text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed sit amet nisi nibh. Quisque aliquam erat et lorem tempus malesuada."
short_text = "Lorem ipsum"
dynamic_label_left = Gtk.Label(short_text) # <-----
dynamic_label_left.set_line_wrap(True)
vbox_left.pack_start(dynamic_label_left, True, True, 0) # should not matter since it's vertical packing
hbox.pack_start(vbox_left, False, False, 0) # <-----
button_right = Gtk.Button("Button")
vbox_right.pack_start(button_right, True, True, 0)
hbox.pack_start(vbox_right, True, True, 0) # <-----
win.connect('delete-event', Gtk.main_quit)
win.show_all()
Gtk.main()
My setup:
- Ubuntu 16.04
- gi.version_info = 3.20
- Python 3.5.2