Python-Ref > GUI programming with PyGTK > Widgets > ButtonBox
 
 

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

ButtonBox

Box widget specialized for button display.
Expand/Shrink
Zdroj: (gtk-buttonbox1-1.py) přispěl(a) Martin Klimt
  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( 500, 360)
 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       # label for horizontal button box
 22       self.label = gtk.Label("")
 23       self.mainbox.pack_start( self.label, padding=10, expand=False)
 24       self.label.show()
 25       # frame1
 26       frame1 = gtk.Frame( "Horizontal Button Box") 
 27       frame1.set_label_align( 0.5, 0.5) 
 28       frame1.set_shadow_type( gtk.SHADOW_OUT)
 29       self.mainbox.pack_start( frame1, padding=0, expand=False)
 30       frame1.show()
 31       # horizontal button box
 32       self.hbutton_box = gtk.HButtonBox()
 33       frame1.add( self.hbutton_box)
 34       # first vbutton
 35       sthbut = gtk.Button("START")
 36       sthbut.connect( "clicked", self.change_start_hb)
 37       self.hbutton_box.add(sthbut)
 38       sthbut.show()
 39       # second vbutton
 40       shbut = gtk.Button("SPREAD")
 41       shbut.connect( "clicked", self.change_spread_hb)
 42       self.hbutton_box.add(shbut)
 43       shbut.show()
 44       # third vbutton
 45       ehbut = gtk.Button("EDGE")
 46       ehbut.connect( "clicked", self.change_edge_hb)
 47       self.hbutton_box.add(ehbut)
 48       ehbut.show()
 49       # fourth vbutton
 50       enhbut = gtk.Button("END")
 51       enhbut.connect( "clicked", self.change_end_hb)
 52       self.hbutton_box.add(enhbut)
 53       enhbut.show()
 54       # fifth vbutton
 55       hb = gtk.Button("Get style")
 56       hb.connect( "clicked", self.change_layout_hb)
 57       self.hbutton_box.add(hb)
 58       hb.show()
 59   
 60       # label for vertical button box
 61       self.label2 = gtk.Label("")
 62       self.mainbox.pack_start( self.label2, padding=10, expand=False)
 63       self.label2.show()
 64       # frame2
 65       frame2 = gtk.Frame( "Vertical Button Box") 
 66       frame2.set_label_align( 0.5, 0.5)
 67       frame2.set_shadow_type( gtk.SHADOW_OUT)    
 68       self.mainbox.pack_start( frame2, padding=0)
 69       frame2.show()
 70       # vertical button box
 71       self.vbutton_box = gtk.VButtonBox()
 72       frame2.add( self.vbutton_box)
 73       # first vbutton
 74       stvbut = gtk.Button("START")
 75       stvbut.connect( "clicked", self.change_start_vb)
 76       self.vbutton_box.add(stvbut)
 77       stvbut.show()
 78       # second vbutton
 79       svbut = gtk.Button("SPREAD")
 80       svbut.connect( "clicked", self.change_spread_vb)
 81       self.vbutton_box.add(svbut)
 82       svbut.show()
 83       # third vbutton
 84       evbut = gtk.Button("EDGE")
 85       evbut.connect( "clicked", self.change_edge_vb)
 86       self.vbutton_box.add(evbut)
 87       evbut.show()
 88       # fourth vbutton
 89       envbut = gtk.Button("END")
 90       envbut.connect( "clicked", self.change_end_vb)
 91       self.vbutton_box.add(envbut)
 92       envbut.show()
 93       # fifth vbutton
 94       vb = gtk.Button("Get style")
 95       vb.connect( "clicked", self.change_layout_vb)
 96       self.vbutton_box.add(vb)
 97       vb.show()
 98   
 99       # show the box
100       self.hbutton_box.show()
101       self.vbutton_box.show()
102       self.mainbox.show()
103   
104     def change_spread_hb (self, hb):
105       self.hbutton_box.set_layout(gtk.BUTTONBOX_SPREAD)
106     def change_edge_hb (self, hb):
107       self.hbutton_box.set_layout(gtk.BUTTONBOX_EDGE) 
108     def change_end_hb (self, hb):
109       self.hbutton_box.set_layout(gtk.BUTTONBOX_END) 
110     def change_start_hb (self, hb):
111       self.hbutton_box.set_layout(gtk.BUTTONBOX_START)     
112   
113     def change_spread_vb (self, vb):
114       self.vbutton_box.set_layout(gtk.BUTTONBOX_SPREAD)
115     def change_edge_vb (self, vb):
116       self.vbutton_box.set_layout(gtk.BUTTONBOX_EDGE) 
117     def change_end_vb (self, vb):
118       self.vbutton_box.set_layout(gtk.BUTTONBOX_END) 
119     def change_start_vb (self, vb):
120       self.vbutton_box.set_layout(gtk.BUTTONBOX_START) 
121   
122     def change_layout_vb (self, vb):
123       v1 = self.vbutton_box.get_layout()
124       if v1 == gtk.BUTTONBOX_SPREAD:
125         self.label2.set_label ("Vertical Button Box is SPREAD")
126       elif v1 == gtk.BUTTONBOX_EDGE:
127         self.label2.set_label ("Vertical Button Box is EDGE") 
128       elif v1 == gtk.BUTTONBOX_START:
129         self.label2.set_label ("Vertical Button Box is START")
130       elif v1 == gtk.BUTTONBOX_END:
131         self.label2.set_label ("Vertical Button Box is END")          
132       else:
133         self.label2.set_label ("Vertical Button Box is DEFAULT")
134   
135     def change_layout_hb (self, hb):
136       h1 = self.hbutton_box.get_layout()
137       if h1 == gtk.BUTTONBOX_SPREAD:
138         self.label.set_label ("Horizontal Button Box is SPREAD")
139       elif h1 == gtk.BUTTONBOX_EDGE:
140         self.label.set_label ("Horizontal Button Box is EDGE") 
141       elif h1 == gtk.BUTTONBOX_START:
142         self.label.set_label ("Horizontal Button Box is START")
143       elif h1 == gtk.BUTTONBOX_END:
144         self.label.set_label ("Horizontal Button Box is END")
145       else:
146         self.label.set_label ("Horizontal Button Box is DEFAULT")
147   
148     def main( self):
149       gtk.main()
150   
151     def destroy( self, w):
152       gtk.main_quit()
153   
154   
155   if __name__ == "__main__":
156     m = MyGUI( "Button Boxes")
157     m.main()
Screenshot:
Program screenshot gtk-buttonbox1-1.pngProgram screenshot gtk-buttonbox1-1a.png
Doba běhu: 1512.9 ms