Chapter 10. Container Widgets

Table of Contents

10.1. The EventBox
10.2. The Alignment widget
10.3. Fixed Container
10.4. Layout Container
10.5. Frames
10.6. Aspect Frames
10.7. Paned Window Widgets
10.8. Viewports
10.9. Scrolled Windows
10.10. Button Boxes
10.11. Toolbar
10.12. Notebooks
10.13. Plugs and Sockets
10.13.1. Plugs
10.13.2. Sockets

10.1. The EventBox

Some GTK widgets don't have associated X windows, so they just draw on their parents. Because of this, they cannot receive events and if they are incorrectly sized, they don't clip so you can get messy overwriting, etc. If you require more from these widgets, the EventBox is for you.

At first glance, the EventBox widget might appear to be totally useless. It draws nothing on the screen and responds to no events. However, it does serve a function - it provides an X window for its child widget. This is important as many GTK widgets do not have an associated X window. Not having an X window saves memory and improves performance, but also has some drawbacks. A widget without an X window cannot receive events, does not perform any clipping on its contents and cannot set its background color. Although the name EventBox emphasizes the event-handling function, the widget can also be used for clipping. (and more, see the example below).

To create a new EventBox widget, use:

  event_box = gtk.EventBox()

A child widget can then be added to this event_box:

  event_box.add(widget) 

The eventbox.py example program demonstrates both uses of an EventBox - a label is created that is clipped to a small box, has a green background and is set up so that a mouse-click on the label causes the program to exit. Resizing the window reveals varying amounts of the label. Figure 10.1, “Event Box Example” illustrates the programs display:

Figure 10.1. Event Box Example

Event Box Example

The source code for eventbox.py is:

    1	#!/usr/bin/env python
    2	
    3	# example eventbox.py
    4	
    5	import pygtk
    6	pygtk.require('2.0')
    7	import gtk
    8	
    9	class EventBoxExample:
   10	    def __init__(self):
   11	        window = gtk.Window(gtk.WINDOW_TOPLEVEL)
   12	        window.set_title("Event Box")
   13	        window.connect("destroy", lambda w: gtk.main_quit())
   14	        window.set_border_width(10)
   15	
   16	        # Create an EventBox and add it to our toplevel window
   17	        event_box = gtk.EventBox()
   18	        window.add(event_box)
   19	        event_box.show()
   20	
   21	        # Create a long label
   22	        label = gtk.Label("Click here to quit, quit, quit, quit, quit")
   23	        event_box.add(label)
   24	        label.show()
   25	
   26	        # Clip it short.
   27	        label.set_size_request(110, 20)
   28	
   29	        # And bind an action to it
   30	        event_box.set_events(gtk.gdk.BUTTON_PRESS_MASK)
   31	        event_box.connect("button_press_event", lambda w,e: gtk.main_quit())
   32	
   33	        # More things you need an X window for ...
   34	        event_box.realize()
   35	        event_box.window.set_cursor(gtk.gdk.Cursor(gtk.gdk.HAND1))
   36	
   37	        # Set background color to green
   38	        event_box.modify_bg(gtk.STATE_NORMAL,
   39	                            event_box.get_colormap().alloc_color("green"))
   40	
   41	        window.show()
   42	
   43	def main():
   44	    gtk.main()
   45	    return 0
   46	
   47	if __name__ == "__main__":
   48	    EventBoxExample()
   49	    main()