Python Forum
[PyGTK] How to center text on multi-line buttons?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGTK] How to center text on multi-line buttons?
#1
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.
Reply
#2
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.
Reply
#3
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()
Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] [Solved]Change text color of one line in TextBrowser Extra 2 4,882 Aug-23-2022, 09:11 PM
Last Post: Extra
  [PyGUI] Creating hot spots in PyGTK DrakeSoft 1 1,605 Aug-01-2022, 04:35 PM
Last Post: Axel_Erfurt
  Line numbers in Text widget rfresh737 3 5,399 Apr-15-2021, 12:30 PM
Last Post: rfresh737
  [PyGTK] Creating Book Table ramazanemreosmanoglu 3 1,838 Oct-27-2020, 06:47 PM
Last Post: Axel_Erfurt
  Part of code is adding extra new line when saving to text file. lovepeace 9 4,999 Aug-24-2019, 12:52 PM
Last Post: lovepeace
  Spacing Between two labels are very far and Top label is not in Center using Tkinter barry76 2 7,058 Jul-30-2019, 10:49 AM
Last Post: wuf
  [Tkinter] Line Number Tkinter Text Are smabubakkar 1 4,375 Jun-22-2019, 07:05 PM
Last Post: woooee
  [Tkinter] How do I center this text in tkinter? ejected 4 63,522 Mar-29-2019, 10:35 PM
Last Post: woooee
  Multi windows in tkinter buttons not responding correctly curtjohn86 13 11,524 Jul-01-2017, 03:42 PM
Last Post: nilamo
  the alternate keyboard commands / center command Luke_Drillbrain 3 4,105 Apr-30-2017, 03:30 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020