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

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

Table layout

How to create a table-like layout.
Combining VBoxes and HBoxes to create the desired layout may be in some cases very tedious. To create a tabular layout it is much simpler to use the Table container.
Expand/Shrink
Zdroj: (gtk-table1-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( 260, -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.table = gtk.Table( 3, 2)
 20       self.window.add( self.table)
 21       # button 1
 22       b = gtk.Button( "Short")
 23       self.table.attach( b, 0, 1, 0, 1)
 24       b.show()
 25       # button 2
 26       b = gtk.Button( "Much longer...")
 27       self.table.attach( b, 0, 1, 1, 2)
 28       b.show()
 29       # button 3
 30       b = gtk.Button( "Big button")
 31       self.table.attach( b, 1, 2, 0, 2)
 32       b.show()
 33       # button 3
 34       b = gtk.Button( "Very long button")
 35       self.table.attach( b, 0, 2, 2, 3)
 36       b.show()
 37       # show the table
 38       self.table.show()
 39   
 40     def main( self):
 41       gtk.main()
 42   
 43     def destroy( self, w):
 44       gtk.main_quit()
 45   
 46   if __name__ == "__main__":
 47     m = MyGUI( "Table example")
 48     m.main()
Screenshot:
Program screenshot gtk-table1-1.png
Doba běhu: 6316.4 ms
Expand/Shrink
Rozdíl proti: gtk-table1-1.py
@@ -24,15 +24,15 @@
     b.show()
     # button 2
     b = gtk.Button( "Much longer...")
-    self.table.attach( b, 0, 1, 1, 2)
+    self.table.attach( b, 0, 1, 1, 2, xpadding=10, ypadding=5) 
     b.show()
     # button 3
     b = gtk.Button( "Big button")
-    self.table.attach( b, 1, 2, 0, 2)
+    self.table.attach( b, 1, 2, 0, 2, yoptions=gtk.EXPAND) 
     b.show()
     # button 3
     b = gtk.Button( "Very long button")
-    self.table.attach( b, 0, 2, 2, 3)
+    self.table.attach( b, 0, 2, 2, 3, xoptions=gtk.EXPAND) 
     b.show()
     # show the table
     self.table.show()
Screenshot:
Program screenshot gtk-table1-2.png
Doba běhu: 1570.9 ms
Expand/Shrink
Rozdíl proti: gtk-table1-2.py
@@ -16,7 +16,7 @@
     self.window.show_all()
   def create_interior( self):
-    self.table = gtk.Table( 3, 2)
+    self.table = gtk.Table( 3, 2, homogeneous=True) 
     self.window.add( self.table)
     # button 1
     b = gtk.Button( "Short")
Screenshot:
Program screenshot gtk-table1-3.png
Doba běhu: 881.6 ms