Python-Ref > GUI programming with PyGTK > Containers and layout > Boxes
 
 

^^->
Klíčová slova
Moduly
Knihovní funkce

Boxes

Simple containers that allow for storage of several GUI items.
When we need to put more than one item into a container that does not support it, such as the Window, we need to use another container that supports it to hold the multiple items and use it as the only child of the Window.
On this page, we will show usage of the simplest such containers - the Boxes. There are two types of boxes - the VBox and the HBox. The usage is the same for both, only the first one orders the items vertically and the other horizontally.
The following examples show completely the same code with the only change in VBox and HBox.
Expand/Shrink
Zdroj: (gtk3-1.py)
  1   # ensure that PyGTK 2.0 is loaded - not an older version
  2   import pygtk
  3   pygtk.require('2.0')
  4   # import the GTK module
  5   import gtk
  6   
  7   class MyGUI:
  8   
  9     def __init__( self, title):
 10       self.window = gtk.Window()
 11       self.title = title
 12       self.window.set_title( title)
 13       self.window.connect( "destroy", self.destroy)
 14       self.create_interior()
 15       self.window.show_all()
 16   
 17     def create_interior( self):
 18       self.mainbox = gtk.VBox()
 19       self.window.add( self.mainbox)
 20       for text in ["Hello world!", "This is a example of VBox packing", "Don't panic"]:
 21         label = gtk.Label( text)
 22         self.mainbox.pack_start( label, padding=10)
 23         label.show()
 24       self.mainbox.show()
 25   
 26     def main( self):
 27       gtk.main()
 28   
 29     def destroy( self, w):
 30       print "Good bye..."
 31       gtk.main_quit()
 32   
 33   
 34   if __name__ == "__main__":
 35     m = MyGUI( "Hello Boxes")
 36     m.main()
stdout:
Good bye...
Screenshot:
Program screenshot gtk3-1.png
Doba běhu: 17868.1 ms
There only a one letter changed in the following code.
Expand/Shrink
Rozdíl proti: gtk3-1.py
@@ -15,7 +15,7 @@
     self.window.show_all()
   def create_interior( self):
-    self.mainbox = gtk.VBox()
+    self.mainbox = gtk.HBox()
     self.window.add( self.mainbox)
     for text in ["Hello world!", "This is a example of VBox packing", "Don't panic"]:
       label = gtk.Label( text)
Zdroj: (gtk3-2.py) +++
stdout:
Good bye...
Screenshot:
Program screenshot gtk3-2.png
Doba běhu: 1899.0 ms