Chapter 9. Miscellaneous Widgets

Table of Contents
9.1. Labels
9.2. Arrows
9.3. The Tooltips Object
9.4. Progress Bars
9.5. Dialogs
9.6. Pixmaps
9.7. Rulers
9.8. Statusbars
9.9. Text Entries
9.10. Spin Buttons
9.11. Combo Box
9.12. Calendar
9.13. Color Selection
9.14. File Selections

9.1. Labels

Labels are used a lot in GTK, and are relatively simple. Labels emit no signals as they do not have an associated X window. If you need to catch signals, or do clipping, place it inside a EventBox widget or a Button widget.

To create a new label, use:
 
label = GtkLabel(str)

The sole argument is the string you wish the label to display.

To change the label's text after creation, use the method:
 
label.set_text(str)

label is the label you created previously, and str is the new string.

The space needed for the new string will be automatically adjusted if needed. You can produce multi-line labels by putting line breaks in the label string.

To retrieve the current string, use:
 
str = label.get()

label is the label you've created, and str is the return string.

The label text can be justified using:
 
label.set_justify(jtype)

Values for jtype are:
 
  JUSTIFY_LEFT
  JUSTIFY_RIGHT
  JUSTIFY_CENTER # the default
  JUSTIFY_FILL

The label widget is also capable of line wrapping the text automatically. This can be activated using:
 
label.set_line_wrap(wrap)

The wrap argument takes a TRUE or FALSE value.

If you want your label underlined, then you can set a pattern on the label:
 
label.set_pattern(pattern)

The pattern argument indicates how the underlining should look. It consists of a string of underscore and space characters. An underscore indicates that the corresponding character in the label should be underlined. For example, the string "__     __" would underline the first two characters and eight and ninth characters.

The label.py program is a short example to illustrate these methods. This example makes use of the Frame widget to better demonstrate the label styles. You can ignore this for now as the Frame widget is explained later on. Figure 9.1 illustrates the result of running the example program:

Figure 9.1 Label Examples

The label.py source code is:
 
    1   #!/usr/bin/env python
    2   
    3   # example label.py
    4   
    5   import gtk
    6   
    7   class Labels:
    8       def __init__(self):
    9           self.window = gtk.GtkWindow(gtk.WINDOW_TOPLEVEL)
   10           self.window.connect("destroy", gtk.mainquit)
   11   
   12           self.window.set_title("Label")
   13           vbox = gtk.GtkVBox(gtk.FALSE, 5)
   14           hbox = gtk.GtkHBox(gtk.FALSE, 5)
   15           self.window.add(hbox)
   16           hbox.pack_start(vbox, gtk.FALSE, gtk.FALSE, 0)
   17           self.window.set_border_width(5)
   18   
   19           frame = gtk.GtkFrame("Normal Label")
   20           label = gtk.GtkLabel("This is a Normal label")
   21           frame.add(label)
   22           vbox.pack_start(frame, gtk.FALSE, gtk.FALSE, 0)
   23     
   24           frame = gtk.GtkFrame("Multi-line Label")
   25           label = gtk.GtkLabel("This is a Multi-line label.\nSecond line\n"
   26                                "Third line")
   27           frame.add(label)
   28           vbox.pack_start(frame, gtk.FALSE, gtk.FALSE, 0)
   29     
   30           frame = gtk.GtkFrame("Left Justified Label")
   31           label = gtk.GtkLabel("This is a Left-Justified\n"
   32                                "Multi-line label.\nThird      line")
   33           label.set_justify(gtk.JUSTIFY_LEFT)
   34           frame.add(label)
   35           vbox.pack_start(frame, gtk.FALSE, gtk.FALSE, 0)
   36     
   37           frame = gtk.GtkFrame("Right Justified Label")
   38           label = gtk.GtkLabel("This is a Right-Justified\nMulti-line label.\n"
   39                                "Fourth line, (j/k)")
   40           label.set_justify(gtk.JUSTIFY_RIGHT)
   41           frame.add(label)
   42           vbox.pack_start(frame, gtk.FALSE, gtk.FALSE, 0)
   43   
   44           vbox = gtk.GtkVBox(gtk.FALSE, 5)
   45           hbox.pack_start(vbox, gtk.FALSE, gtk.FALSE, 0)
   46           frame = gtk.GtkFrame("Line wrapped label")
   47           label = gtk.GtkLabel("This is an example of a line-wrapped label.  It "
   48                                "should not be taking up the entire             "
   49                                "width allocated to it, but automatically "   50                                "wraps the words to fit.  "
   51                                "The time has come, for all good men, to come to "
   52                                "the aid of their party.  "
   53                                "The sixth sheik's six sheep's sick.\n"
   54                                "     It supports multiple paragraphs correctly, "
   55                                "and  correctly   adds "
   56                                "many          extra  spaces. ")
   57           label.set_line_wrap(gtk.TRUE)
   58           frame.add(label)
   59           vbox.pack_start(frame, gtk.FALSE, gtk.FALSE, 0)
   60     
   61           frame = gtk.GtkFrame("Filled, wrapped label")
   62           label = gtk.GtkLabel("This is an example of a line-wrapped, filled label.  "
   63                                "It should be taking "
   64                                "up the entire              width allocated to it.  "
   65                                "Here is a sentence to prove "
   66                                "my point.  Here is another sentence. "
   67                                "Here comes the sun, do de do de do.\n"
   68                                "    This is a new paragraph.\n"
   69                                "    This is another newer, longer, better "
   70                                "paragraph.  It is coming to an end, "
   71                                "unfortunately.")
   72           label.set_justify(gtk.JUSTIFY_FILL)
   73           label.set_line_wrap(gtk.TRUE)
   74           frame.add(label)
   75           vbox.pack_start(frame, gtk.FALSE, gtk.FALSE, 0)
   76     
   77           frame = gtk.GtkFrame("Underlined label")
   78           label = gtk.GtkLabel("This label is underlined!\n"
   79                                "This one is underlined in quite a funky fashion")
   80           label.set_justify(gtk.JUSTIFY_LEFT)
   81           label.set_pattern(
   82               "_________________________ _ _________ _ ______     __ _______ ___")
   83           frame.add(label)
   84           vbox.pack_start(frame, gtk.FALSE, gtk.FALSE, 0)
   85           self.window.show_all ()
   86   
   87   def main():
   88       gtk.mainloop()
   89       return 0
   90   
   91   if __name__ == "__main__":
   92       Labels()
   93       main()