Python Forum

Full Version: [PyGTK] How to center text on multi-line buttons?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Title pretty much says it all; if I make a button in GTK with a text label that has line-break(s) in it, the text on the label ends up being left aligned - and I would like it to be centered.

Longer version:

There is a set_justify() method on <label>s, which would accept "center" as a value - but the <button><label> is a <property> of the <button>, not a child. Still, looking at the result with the GTKInspector, I can clearly see that a label gets created inside the button, though I have found no way to attach a class or ID to such an implicit <label>, or to access it programmatically. For example, builder.get_object("button1").get_property("label") returns a string, and not the actual label object, so there's nothing for me to call the set_justify() method on. Furthermore, while I can call set_alignment() on the button itself, this only changes the alignment of the <label> bounding box as a whole, not the lines of text within it. I have spent hours looking for a solution and have come up short - so I have to ask for help!

Edit: As far as I have been able to determine, there is no way to achieve this with CSS alone.
You use justify to left/right/center justify the text inside the label widget. To center the label widget inside the button you use alignment. There was a button.set_alignment(x, y) that did this but it is depreciated or gone. button.set_alignment(0.5, 0.5) would center the label widget inside the button. Now I think you use set_halign and set_valign on the label widget inside the button.
try this (change "icon.png" to your icon name)

import gi

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


class ButtonWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="Button Demo")
        self.set_border_width(10)

        hbox = Gtk.Box(spacing=6)
        self.add(hbox)

        button = Gtk.Button()
        
        grid = Gtk.Grid ()
        img = Gtk.Image()
        img.set_from_file("icon.png")
        label = Gtk.Label (label='test button\nwith two lines')
        label.set_justify(Gtk.Justification.CENTER)

        grid.attach (img, 0, 0, 1, 1)
        grid.attach (label, 0, 1, 1, 1)
        grid.show_all ()    

        button.add (grid)
        
        
        button.connect("clicked", self.on_click_me_clicked)
        hbox.pack_start(button, True, True, 0)


    def on_click_me_clicked(self, button):
        print('"Click me" button was clicked')

        Gtk.main_quit()


win = ButtonWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
Thanks guys! I think I found a better solution by editing the XML and adding an explicit <label> child to the <button>:
<object class="GtkButton">
  <child>
    <object class="GtkLabel">
      <property name="justify">center</property>
      <property name="label">Two&#xD;Lines</property>
    </object>
  </child>
</object>
Probably obvious to the more experienced, but I'm only just getting my feet wet with this.