10.3. Fixed Container

The Fixed container allows you to place widgets at a fixed position within it's window, relative to it's upper left hand corner. The position of the widgets can be changed dynamically.

There are only three calls associated with the fixed widget:
 
fixed = GtkFixed()

fixed.put(widget, x, y)

fixed.move(widget, x, y)

The function GtkFixed() allows you to create a new Fixed container.

The put() method places widget in the container fixed at the position specified by x and y.

The move() method allows the specified widget to be moved to a new position.

The fixed.py example illustrates how to use the Fixed Container. Figure 10.2 shows the result:

Figure 10.2 Fixed Example

The source code for fixed.py:
 
    1   #!/usr/bin/env python
    2   
    3   # example fixed.py
    4   
    5   import gtk
    6   
    7   class FixedExample:
    8       # This callback method moves the button to a new position
    9       # in the Fixed container.
   10       def move_button(self, widget):
   11           self.x = (self.x+30)%300
   12           self.y = (self.y+50)%300
   13           self.fixed.move(widget, self.x, self.y) 
   14   
   15       def __init__(self):
   16           self.x = 50
   17           self.y = 50
   18   
   19           # Create a new window
   20           window = gtk.GtkWindow(gtk.WINDOW_TOPLEVEL)
   21           window.set_title("Fixed Container")
   22   
   23           # Here we connect the "destroy" event to a signal handler 
   24           window.connect("destroy", gtk.mainquit)
   25    
   26           # Sets the border width of the window.
   27           window.set_border_width(10)
   28   
   29           # Create a Fixed Container
   30           self.fixed = gtk.GtkFixed()
   31           window.add(self.fixed)
   32           self.fixed.show()
   33     
   34           for i in range(1, 4):
   35               # Creates a new button with the label "Press me"
   36               button = gtk.GtkButton("Press me")
   37     
   38               # When the button receives the "clicked" signal, it will call the
   39               # method move_button().
   40               button.connect("clicked", self.move_button)
   41     
   42               # This packs the button into the fixed containers window.
   43               self.fixed.put(button, i*50, i*50)
   44     
   45               # The final step is to display this newly created widget.
   46               button.show()
   47   
   48           # Display the window
   49           window.show()
   50   
   51   def main():
   52       # Enter the event loop
   53       gtk.mainloop()
   54       return 0
   55   
   56   if __name__ == "__main__":
   57       FixedExample()
   58       main()