Search code examples
python-2.7gtk3pygobjectpango

How to set the alignment, or how to find PyGObject documentation


I am trying to right-align some text content in a CellRenderer. Through several searches I found two approaches, but these do not work. With bold-setting, you need to enable this feature first, so I am guessing I also need to enable alignment setting, but have not found how to do this. This runs without exception:

r = Gtk.CellRendererText()
r.props.width_chars = 10
r.set_property('alignment', Pango.Alignment.RIGHT) # no effect
r.props.alignment = Pango.Alignment.RIGHT # no effect
r.props.weight_set = True # not really needed
r.props.weight = Pango.Weight.BOLD # works, output is bold

This is what I guessed from the bold example but does NOT work:

 r.props.alignment_set = True

The error is: 'gi._gobject.GProps' object has no attribute 'alignment_set'

Looking at these references I do not find something on GProps:

This resource does say something about alignment, but it is unclear to me how to convert this C code to Python/GObject:

My question is how to fix this problem, where is the ref manual for this PyGObject error message, or how should I code the right-alignment?


Update: I am currently looking at this similar SO question for clues.

As for the comment of TingPing, I looked at the set_alignment() method,and tried these:

r.set_alignment(Pango.Alignment.RIGHT) # error: set_alignment() takes exactly 3 arguments (2 given)
r.set_alignment(200, 0) # no error, no effect

besides this method seems intended to create some pixels padding at the left, which is not what I need: align text to the right of the cell space.


Update:

Perhaps the above code is good, but perhaps the CellRenderer() has no intrinsic width, no excess space to put to the left of the content. I thought of this because of thinking of simply left-padding my numberic cell content with spaces. Then I need to decide on the maximum field length. Perhaps the CellRenderer does not 'know' about a default field length. I added a settinge to .props.width_chars, but this did unfortunately not cause any rignt-alignment.


Solution

  • Through this C example I tried this to right-align:

    r.props.xalign = 1.0
    

    and it works! The xalign is the fraction of free space 0..1.0 to put to the left of the text. The value 0.5 will center the text.