Python-Ref > GUI programming with PyGTK > Data structures > Adjustments
 
 

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

Adjustments

Objects that define how scrollbars, scale widgets, etc. behave.
There are several widgets in GTK which let the user adjust their value in some range. This is done mostly by dragging a slider alongside the widget, by can be accomplished in a different matter. Also widgets that display potentially large amount of data, such as the TextView, need to somehow store the information about actively displayed part of the widget and pass this information to the rest of the system.
To enable consistent management of such adjustable widgets a special object was introduced. The Adjustment object is not part of a GUI and cannot be displayed, it is just a storage for data related to the adjustment. It stores the current value of the adjustment, information about upper and lower limit of the adjustment and size of step to use when adjustment is incremented of decremented.
We will demonstrate basic adjustment features on a scrollbar.
Expand/Shrink
  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( 200, 100)
 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 value display
 22       self.label = gtk.Label()
 23       self.label.show()
 24       # adjustment
 25       adj = gtk.Adjustment( value=10, lower=0, upper=30, step_incr=1, page_incr=10, page_size=0) 
 26       adj.connect( "value_changed", self.adjustment_changed) 
 27       adj.emit( "value_changed") # force update of label 
 28       # scrollbar - we provide our own Adjustment, otherwise scrollbar creates a new one for us
 29       sb = gtk.HScrollbar( adjustment=adj) 
 30       sb.show()
 31       # pack the widgets
 32       self.mainbox.pack_start( self.label)
 33       self.mainbox.pack_start( sb, expand=False)
 34       # show the box
 35       self.mainbox.show()
 36   
 37     def adjustment_changed( self, adj):
 38       self.label.set_label( "%.2f" % adj.value)
 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( "Adjustment example")
 48     m.main()
Screenshot:
Program screenshot gtk-adjustment1-1.pngProgram screenshot gtk-adjustment1-1a.png
Doba běhu: 1969.8 ms
The next example shows synchronization of two scrollbars using the same adjusment and "manual" incrementing of the slider by means of buttons.
Expand/Shrink
  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( 200, 100)
 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 value display
 22       self.label = gtk.Label()
 23       self.label.show()
 24       # adjustment
 25       self.adj = gtk.Adjustment( value=10, lower=0, upper=30, step_incr=1, page_incr=10, page_size=0)
 26       self.adj.connect( "value_changed", self.adjustment_changed)
 27       self.adj.emit( "value_changed") # force update of label
 28       # scrollbar - we provide our own Adjustment, otherwise scrollbar creates a new one for us
 29       sb = gtk.HScrollbar( adjustment=self.adj) 
 30       sb.show()
 31       # add a second scrollbar, using the same adjustment
 32       sb2 = gtk.HScrollbar( adjustment=self.adj) 
 33       sb2.show()
 34       # pack the widgets
 35       self.mainbox.pack_start( sb2, expand=False)
 36       self.mainbox.pack_start( self.label)
 37       self.mainbox.pack_start( sb, expand=False)
 38       # add buttons for value adjustment
 39       box = gtk.HBox()
 40       self.mainbox.pack_start( box, expand=False)
 41       b = gtk.Button( "-3")
 42       b.connect( "clicked", self.increment_adjustment, -3)
 43       b.show()
 44       box.pack_start( b)
 45       b = gtk.Button( "+3")
 46       b.connect( "clicked", self.increment_adjustment, 3)
 47       b.show()
 48       box.pack_start( b)
 49       # show the box
 50       self.mainbox.show()
 51   
 52     def increment_adjustment( self, w, increment):
 53       new_value = self.adj.value + increment 
 54       # take care of value getting out of range
 55       if new_value > self.adj.upper:
 56         new_value = self.adj.upper
 57       elif new_value < self.adj.lower:
 58         new_value = self.adj.lower
 59       self.adj.set_value( new_value) 
 60       
 61   
 62     def adjustment_changed( self, adj):
 63       self.label.set_label( "%.2f" % adj.value)
 64   
 65     def main( self):
 66       gtk.main()
 67   
 68     def destroy( self, w):
 69       gtk.main_quit()
 70   
 71   if __name__ == "__main__":
 72     m = MyGUI( "Adjustment example")
 73     m.main()
Screenshot:
Program screenshot gtk-adjustment1-2.pngProgram screenshot gtk-adjustment1-2a.png
Doba běhu: 1715.8 ms
It is also possible to change other properties of the adjustment, such as its limits.
Expand/Shrink
  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( 200, 100)
 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 value display
 22       self.label = gtk.Label()
 23       self.label.show()
 24       # adjustment
 25       self.adj = gtk.Adjustment( value=10, lower=0, upper=30, step_incr=1, page_incr=10, page_size=0) 
 26       self.adj.connect( "value_changed", self.adjustment_changed)
 27       self.adj.connect( "changed", self.adjustment_changed) # something has chaged 
 28       self.adj.emit( "value_changed") # force update of label
 29       # scrollbar - we provide our own Adjustment, otherwise scrollbar creates a new one for us
 30       sb = gtk.HScrollbar( adjustment=self.adj)
 31       sb.show()
 32       # pack the widgets
 33       self.mainbox.pack_start( self.label)
 34       self.mainbox.pack_start( sb, expand=False)
 35       # add buttons for adjustment bound adjustment
 36       box = gtk.HBox()
 37       self.mainbox.pack_start( box, expand=False)
 38       b = gtk.Button( "-10")
 39       b.connect( "clicked", self.change_adjusment_upper_bound, -10)
 40       b.show()
 41       box.pack_start( b)
 42       b = gtk.Button( "+10")
 43       b.connect( "clicked", self.change_adjusment_upper_bound, 10)
 44       b.show()
 45       box.pack_start( b)
 46       # show the box
 47       self.mainbox.show()
 48   
 49     def adjustment_changed( self, adj):
 50       self.label.set_markup( "%.2f  -  <b>%.2f</b>  -  %.2f" % (adj.lower, adj.value, adj.upper))
 51   
 52     def change_adjusment_upper_bound( self, w, amount):
 53       upper = self.adj.upper + amount
 54       if upper > self.adj.lower:
 55         # update only if its is possible
 56         self.adj.upper = upper
 57         # take care of possible "outing" of the slider
 58         if self.adj.value > self.adj.upper:
 59           self.adj.value = self.adj.upper
 60         # emit "changed" so that the widget updates itself 
 61         self.adj.changed() 
 62   
 63     def main( self):
 64       gtk.main()
 65   
 66     def destroy( self, w):
 67       gtk.main_quit()
 68   
 69   if __name__ == "__main__":
 70     m = MyGUI( "Adjustment example")
 71     m.main()
Screenshot:
Program screenshot gtk-adjustment1-3.pngProgram screenshot gtk-adjustment1-3a.png
Doba běhu: 1478.8 ms