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

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

Boxes in more detail

How can we influence the appearance and behaviour of boxes.
Expand/Shrink
Zdroj: (gtk5-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.set_size_request( 300, -1)
 14       self.window.connect( "destroy", self.destroy)
 15       self.create_interior()
 16       self.window.show_all()
 17   
 18     def create_interior( self):
 19       self.mainbox = gtk.VBox()
 20       self.window.add( self.mainbox)
 21       self.add_hbox( False, False, False)
 22       self.add_hbox( False, True, False)
 23       self.add_hbox( False, False, True)
 24       self.add_hbox( False, True, True)
 25       self.add_hbox( True, False, False)
 26       self.add_hbox( True, True, False)
 27       self.add_hbox( True, True, True)
 28       self.mainbox.show()
 29   
 30     def add_hbox( self, myhomegenous, myexpand, myfill):
 31       l = gtk.Label()
 32       l.set_markup("<b>Homo</b>: %s, <b>Expand</b>: %s, <b>Fill</b>: %s" % ( myhomegenous, myexpand, myfill))
 33       l.show()
 34       self.mainbox.pack_start( l, padding=3)
 35       box = gtk.HBox( homogeneous=myhomegenous, spacing=0)
 36       for word in ["A","AB","ABC","ABCDE"]:
 37         b = gtk.Button( word)
 38         box.pack_start( b, expand=myexpand, fill=myfill, padding=0)
 39         b.show()
 40       self.mainbox.pack_start( box)
 41       box.show()
 42   
 43     def main( self):
 44       gtk.main()
 45   
 46     def destroy( self, w):
 47       gtk.main_quit()
 48   
 49   
 50   if __name__ == "__main__":
 51     m = MyGUI( "Hello Boxes")
 52     m.main()
Screenshot:
Program screenshot gtk5-1.png
Doba běhu: 7129.9 ms
Expand/Shrink
Zdroj: (gtk5-2.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.set_size_request( -1, -1)
 14       self.window.connect( "destroy", self.destroy)
 15       self.create_interior()
 16       self.window.show_all()
 17   
 18     def create_interior( self):
 19       self.mainbox = gtk.VBox()
 20       self.window.add( self.mainbox)
 21       for x in range( 4):
 22         box = gtk.HBox()
 23         i = 0
 24         for letter in "ABCDE":
 25           b = gtk.Button( letter)
 26           if i <= x:
 27             box.pack_start( b, expand=False, fill=False)
 28           else:
 29             box.pack_end( b, expand=False, fill=False)
 30           b.show()
 31           i += 1
 32         box.show()
 33         self.mainbox.pack_start( box)
 34       self.mainbox.show()
 35   
 36     def add_hbox( self, myhomegenous, myexpand, myfill):
 37       box = gtk.HBox()
 38       for word in ["A","AB","ABC","ABCDE"]:
 39         b = gtk.Button( word)
 40         box.pack_start( b, expand=myexpand, fill=myfill, padding=0)
 41         b.show()
 42       self.mainbox.pack_start( box)
 43       box.show()
 44   
 45     def main( self):
 46       gtk.main()
 47   
 48     def destroy( self, w):
 49       gtk.main_quit()
 50   
 51   
 52   if __name__ == "__main__":
 53     m = MyGUI( "Hello Boxes")
 54     m.main()
Screenshot:
Program screenshot gtk5-2.pngProgram screenshot gtk5-2a.png
Doba běhu: 2971.1 ms