[pygtk] Left justifying label text

Timo timomlists at gmail.com
Mon Mar 7 01:43:16 WST 2011


On 06-03-11 15:18, mercado wrote:
> Hello all,
>
> I am having a problem left justifying the text in a label.  I have
> included a sample program below.  I am trying to left justify the
> label text ("This is a test"), but it is showing it centered instead.
>
> Can anybody point out where I am going wrong?  Thanks in advance.
Have you read the docs about set_justify()? It has 3 important parts in 
your case.
Quote [1]:
|gtk.JUSTIFY_LEFT| is the default value when the widget is first 
created. If you want to set the alignment of the label as a whole, use 
the |gtk.Misc.set_alignment|() 
<http://library.gnome.org/devel/pygtk/stable/class-gtkmisc.html#method-gtkmisc--set-alignment> 
method instead. The |set_justify|() 
<http://library.gnome.org/devel/pygtk/stable/class-gtklabel.html#method-gtklabel--set-justify> 
has no effect on labels containing only a single line.

  * Your call to set_justify() does nothing, because you call the 
default value.
  * It wouldn't work anyway because you only have one line.
  * The solution is to use set_alignment()

So replace label.set_justify(gtk.JUSTIFY_LEFT) with 
label.set_alignment(0, .5) and it should work.

Cheers,
Timo

[1] 
http://library.gnome.org/devel/pygtk/stable/class-gtklabel.html#method-gtklabel--set-justify

> --------------------------------------------------------------------------------
> import gtk
>
> def main():
>      window = gtk.Window(gtk.WINDOW_TOPLEVEL)
>      window.set_title("Left Justify Label")
>      window.set_size_request(500, 500)
>
>      frame = gtk.Frame("Status")
>
>      label = gtk.Label("This is a test")
>      label.set_justify(gtk.JUSTIFY_LEFT)
>
>      frame.add(label)
>
>      vbox = gtk.VBox(homogeneous=False, spacing=0)
>      vbox.pack_start(frame, False, False, 0)
>
>      window.add(vbox)
>      window.connect("destroy", gtk.main_quit)
>      window.show_all()
>
>      gtk.main()
>
> if __name__ == "__main__":
>      main()
> _______________________________________________
> pygtk mailing list   pygtk at daa.com.au
> http://www.daa.com.au/mailman/listinfo/pygtk
> Read the PyGTK FAQ: http://faq.pygtk.org/



More information about the pygtk mailing list