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

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

Scales

A simple slider widget for selection of value in a range.
There are two scale widgets in GTK - VScale and HScale. The only difference between them is the orientation in which they appear. The scale use an Adjustments to store and share their value.
The simplest example of a scale widget is very similar to the Adjustment example code.
Expand/Shrink
Zdroj: (gtk-scale1-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( 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       # the scale
 29       scale = gtk.HScale( adjustment=adj) 
 30       scale.show()
 31       # pack the widgets
 32       self.mainbox.pack_start( self.label)
 33       self.mainbox.pack_start( scale, 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( "Scale example")
 48     m.main()
Screenshot:
Program screenshot gtk-scale1-1.pngProgram screenshot gtk-scale1-1a.png
Doba běhu: 1820.0 ms
Expand/Shrink
Zdroj: (gtk-scale1-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       # label for value display
 22       self.img = gtk.Image()
 23       self.img.set_from_file( "penguin.jpg")
 24       self.img.show()
 25       self.mainbox.pack_start( self.img)
 26       self.pixbuf = self.img.get_pixbuf() 
 27       # adjustment
 28       adj = gtk.Adjustment( value=100, lower=10, upper=300, step_incr=1, page_incr=10, page_size=0)
 29       adj.connect( "value_changed", self.adjustment_changed)
 30       adj.emit( "value_changed") # force update of label
 31       # the scale
 32       scale = gtk.HScale( adjustment=adj) 
 33       scale.show()
 34       self.mainbox.pack_start( scale, expand=False)
 35       # show the box
 36       self.mainbox.show()
 37   
 38     def adjustment_changed( self, adj):
 39       x = int( self.pixbuf.get_width() * adj.value / 100)
 40       y = int( self.pixbuf.get_height() * adj.value / 100)
 41       new = self.pixbuf.scale_simple( x, y, gtk.gdk.INTERP_BILINEAR) 
 42       self.img.set_from_pixbuf( new) 
 43   
 44     def main( self):
 45       gtk.main()
 46   
 47     def destroy( self, w):
 48       gtk.main_quit()
 49   
 50   if __name__ == "__main__":
 51     m = MyGUI( "Scale example")
 52     m.main()
Screenshot:
Program screenshot gtk-scale1-2.pngProgram screenshot gtk-scale1-2a.png
Doba běhu: 595.5 ms