4

A gtk.Label can't be aligned in center when line-wrap is turned on. Example code:

import pygtk
pygtk.require('2.0')
import gtk

class testwin(gtk.Window):
   def __init__(self):
      gtk.Window.__init__(self)
      width,height = 300,300
      self.set_size_request(width,height)
      self.set_position(gtk.WIN_POS_CENTER)
      self.set_title("test window")

      label = gtk.Label("test text")
      label.set_line_wrap(True)
      label.set_justify(gtk.JUSTIFY_CENTER)
      label.set_alignment(0.5,0.5)
      label.connect("size-allocate",lambda l,s: l.set_size_request(s.width-1, -1))

      self.add(label)
      self.connect("destroy", gtk.main_quit)
      self.show_all()

testwin()
gtk.main()

It looks like this, that means, it's aligned left: http://m45.img-up.net/?up=pic122x97.png

If you comment out line 14 (set_line_wrap) everything is perfectly fine: http://o54.img-up.net/?up=pic2y00p9.png

Please note that yalign works fine. So it seems like the first argument in the gtk.Misc.set_alignment-function has no effect when line wrap is turned on.

Using Fedora 16, 64bit, gtk 3.2.4, pygtk 2.24.0, python 2.7.2

Question: Is this intended or a bug? How is it supposed to be made or is a workaround available?

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
thomas
  • 561
  • 2
  • 17
  • By the way, the behaviour is somehow related to set_size_request. But I need this line, since in my usecase the wrapping is supposed to adapt to window resizing. – thomas Apr 07 '12 at 16:48
  • This happens to me on GTK+ 2.24 (C) as well. – Didi Kohen Dec 24 '14 at 13:34

1 Answers1

2

This isn't really an answer, but it doesn't fit in a comment.

I'm going to guess this is a bug. When I convert your code to run on PyGObject (PyGTK's successor), it works as you'd expect.

from gi.repository import Gtk

class testwin(Gtk.Window):
   def __init__(self):
      Gtk.Window.__init__(self)
      width,height = 300,300
      self.set_size_request(width,height)
      self.set_position(Gtk.WindowPosition.CENTER)
      self.set_title("test window")

      label = Gtk.Label("test text")
      label.set_line_wrap(True)
      label.set_justify(Gtk.Justification.CENTER)
      label.set_alignment(0.5,0.5)
      label.connect("size-allocate",lambda l,s: l.set_size_request(s.width-1, -1))

      self.add(label)
      self.connect("destroy", Gtk.main_quit)
      self.show_all()

testwin()
Gtk.main()

Of course, if you're stuck on PyGTK and you can't switch over to PyGObject right away, then this answer doesn't solve your problem.

dumbmatter
  • 9,351
  • 7
  • 41
  • 80
  • Interesting to know. In this special case, it will not help though. The code is part of a rather big PyGtk-application. At the moments there are no plans to migrate the whole thing to PyGObject. But just out of interest: Is there a complete documentation of the new objects in PyGObject? – thomas Apr 07 '12 at 20:56
  • Not that I've seen, although you can use the normal GTK+ documentation a lot of the time. Also, I find changes from PyGTK by looking at [the automatic conversion script](http://git.gnome.org/browse/pygobject/tree/pygi-convert.sh), which has most of the changes listed. – dumbmatter Apr 07 '12 at 21:02
  • Thanks for the reference. Unfortunately, PyGObject is still not really usable. Many things are documented nowhere and others are even defective (see http://stackoverflow.com/questions/6133622/using-cairo-regions-in-python-with-gi-repository). – thomas Apr 07 '12 at 22:05
  • 1
    I know this is old, but if anyone (including me) finds this thread looking for a documentation on PyGObject: https://lazka.github.io/pgi-docs is the place to go. – charlie May 23 '19 at 17:59