0

I've got a Gtk.Label contained within a Gtk.Grid something like as follows:

    self.__grid = Gtk.Grid()
    self.__grid.set_row_spacing(2)
    self.__grid.set_column_spacing(5)
    self.__grid.props.margin = 5
#...
    self.clock_label = Gtk.Label("N/A")
    self.clock_label.set_halign(Gtk.Align.START)
    self.__grid.attach(self.clock_label, current_col, current_row, 1, 1)

Which works as expected. I encountered an unwanted behavior, though, which meant that as the seconds ticked by, the text of the label being set elsewhere would offset the column (as the clock label is the widest thing in this column) causing all elements to the right of it to shift back and forth as the string itself grew wider (with seconds like 8) or narrower (with seconds like 1).

To solve this, I added a line to the setup code for this label

    self.clock_label.set_width_chars(8)

And voila, it worked. However, it insists on centering the text when I do that, irrespective of the presence or absence of the set_halign().

I would like very much to have a label whose text starts on the left-hand border of its cell, but whose width is also padded such that changes in string length are of no concern.

Any help is appreciated. =)

pdm
  • 1,027
  • 1
  • 9
  • 24

1 Answers1

0

I found the answer here after SO gave it to me in the Related column. Turns out that xalign (with a parameter of 0.0) butts the label up against the left boundary just as I wanted. Hooray.

Though, I don't see anywhere that halign is actually depreciated. Here or here. Is it not, then? This is a touch confusing.

pdm
  • 1,027
  • 1
  • 9
  • 24
  • 1
    `halign` is common to all widgets and affects the **widget** alignment. `xalign` is specific to `GtkLabel` and affects the **text** alignment. The [documentation](https://developer.gnome.org/gtk3/stable/GtkLabel.html#GtkLabel--xalign) says the same thing (although IMO in a quite cryptical form). – ntd Apr 03 '19 at 04:48