Weird behaviour of Gtk Wrap

Im developing an application and so far have been facing weird issues that I don’t even want my enemy to face.

Source code
import gi

gi.require_version("Gtk", "3.0")
from gi.repository import Gdk, Gtk

class StatusLabel:
    def __init__(self, text, show_progress):

        self.label = Gtk.Label(
            name="promptframe-statuslabel",
            label=text,
            valign=1)
            use_markup=True,  #Comment this line
            wrap=True) #Comment this line
        self.spin = Gtk.Spinner()
        if show_progress:
            self.spin.start()
        self.grid = Gtk.Grid(column_spacing=2)
        self.grid.attach(self.label, 0, 0, 1, 1)
        self.grid.attach(self.spin, 1, 0, 1, 1)

    def set_text(self, label):

        self.label.set_text(label)

    def set_name(self, name):

        self.label.set_name(name)

    def start_spin(self):

        self.spin.start()

    def stop_spin(self):

        self.spin.stop()

    def get_widget(self):

        return self.grid

box = Gtk.Box(orientation=1, valign=1)
ent = Gtk.Entry(text=">>> ", valign=1)
box.pack_start(ent, 1, 1, 1)
def add_status(wid, event):
    keyname = Gdk.keyval_name(event.keyval)
    if keyname != "Control_R":
        return
    text = ent.get_text()
    text = text + text[::-1] + text.upper() + text.lower() #Some long text
    st = StatusLabel(text=text, show_progress=False)
    box.pack_end(st.get_widget(), 1, 1, 1)
    box.show_all()

ent.connect("key-press-event", add_status)
win = Gtk.Window()
win.connect("destroy", Gtk.main_quit)
win.add(box)
win.show_all()
Gtk.main()

The problem is why does the wrap mode add some extra space below it. I produced a minimal working example by adding an entry inside a box. Type some text inside it and press control_r. Now some text will be displayed but you can see that there is some extra space added at last. You can’t even resize the window to delete that space.

Now just comment the mentioned lines in source code and some text, viola! There is no additional space. Beautiful.

Now someone please help me why is it happening like this.
And another weird issue I faced is (this example doesn’t reproduce it), sometimes, the cursor disappears inside my application window. Or it simply freezes. Then GNOME shows me that my application isn’t responding and I kill it.

Any help appreciated :slight_smile:

https://wiki.gnome.org/HowDoI/Labels

1 Like

Thanks that helped. Now my label is always at center, any rescue to that?

Depending on the situation, set the halign and valign properties (from GtkWidget) or the xalign and yalign properties (from GtkLabel)

Tried, didn’t work fine

The chapter on GtkBox versus GtkGrid: sizing will probably be helpful here. Also note that for GtkLabel, the xalign/yalign properties are easy to overlook in favour of the halign/valign properties, but these are different.

The chapter I linked has some diagrams and further explanation about how box/grid alignment, spacing, expansion, etc work like below:

1 Like

I appreciate your help. But I’m confused as these properties aren’t working when the Label has wrap enabled.

Sorry, but you’ll have to be more specific. If your label has enough text to need wrapping into several lines, then aligning it left/or right doesn’t make much sense, because it fills the whole line to require wrapping.

Are you now talking about justification of text?

1 Like

Sorry, you can see the the Glade source below, which reproduces the problem. The text appears on center, even though its justification is left.

Source
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
  <requires lib="gtk+" version="3.20"/>
  <object class="GtkWindow">
    <property name="can_focus">False</property>
    <child>
      <placeholder/>
    </child>
    <child>
      <object class="GtkGrid">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <child>
          <object class="GtkLabel">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="halign">start</property>
            <property name="valign">start</property>
            <property name="label" translatable="yes">Text is on centre, don't know why !</property>
            <property name="wrap">True</property>
            <property name="wrap_mode">word-char</property>
            <property name="width_chars">50</property>
          </object>
          <packing>
            <property name="left_attach">0</property>
            <property name="top_attach">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkSpinner">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="active">True</property>
          </object>
          <packing>
            <property name="left_attach">1</property>
            <property name="top_attach">0</property>
          </packing>
        </child>
        <child>
          <placeholder/>
        </child>
        <child>
          <placeholder/>
        </child>
      </object>
    </child>
  </object>
</interface>

And thank you, while preparing this example for you, I accidentally looked at xalign property of Label and set it to zero, and it worked ! Never knew we had such property for widget. :smile:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.