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

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

SpinButton

Entry for numbers with arrows to increase and decrease the value.
SpinButton is created by calling gtk.SpinButton() function with desired attributes. Info about its range and behaviour are held by associated Adjustment object.
Function call spin_button.configure(adj, climb_rate=0.0, digits=0) may be used later for reconfiguring SpinButton. Settings for both objects involved are sumarized in following tables:
Settings for Adjustment object
value initial value for the Spin Button
lower lower range value
upper upper range value
step_increment value to increment/decrement when pressing or mouse button-1 on a button
page_increment value to increment/decrement when pressing or mouse button-2 on a button
page_size unused
Settings for SpinButton object
adjustment the adjustment that holds the value of the spinbutton
climb-rate the acceleration rate when you hold down a button
digits the number of decimal places to display
numeric if True, non-numeric characters should be ignored
snap-to-ticks if True, erroneous values are automatically changed to a spin button's nearest step increment
update-policy either gtk.UPDATE_ALWAYS (the spin button should update always), or gtk.UPDATE_IF_VALID the spin button should update only when the value is legal)
value the current value
wrap if True, a spin button should wrap upon reaching its limits
Expand/Shrink
  1   import pygtk
  2   pygtk.require('2.0')
  3   import gtk
  4   
  5   
  6   class MyGUI:
  7     """PyGTK HelloWorld programm for SpinButton widget."""
  8     
  9     def __init__(self, title):
 10       self.window = gtk.Window()
 11       self.window.connect("destroy", lambda w: gtk.main_quit())
 12       self.window.set_title(title)
 13       self.window.set_size_request(200, 50)
 14       self.create_interior()   # "create" window content
 15       self.window.show_all()
 16     
 17     def create_interior(self):
 18       # prepare SpinButton:
 19       # a) info about its range and behaviour are held by associated Adjustment (page_size attribute is not used)
 20       adj = gtk.Adjustment(value=0, lower=-100, upper=100, step_incr=1, page_incr=10)
 21       # b) SpinButton's specific properties are part of its constructor
 22       spin_button = gtk.SpinButton(adj, climb_rate=0.0, digits=0)
 23       # append SpinButton to window
 24       self.mainbox = spin_button
 25       self.window.add(self.mainbox)
 26       self.mainbox.show()
 27     
 28     def main(self):
 29       gtk.main()
 30   
 31   
 32   if __name__ == "__main__":
 33     m = MyGUI("SpinButton example")
 34     m.main()
Screenshot:
Program screenshot gtk-spinbutton1-1.jpg
Doba běhu: 1194.9 ms
This example goes a little further. First the SpinButton is created (as in the first example) and then one additional property is set via its setter - wrap (which means that the SpinButton after achieving top or bottom limit will overflow to the other side).
Next the Button is associated with function handler for event clicked, which gets actual value and outputs itto the dedicated Label on the canvas.
Thanks to SpinButton's function get_value_as_int() actual value (which is allowed to be real in the constructor) is rounded off and therefore outputted as Integer.
Expand/Shrink
  1   import pygtk
  2   pygtk.require('2.0')
  3   import gtk
  4   
  5   
  6   class MyGUI:
  7     """PyGTK HelloWorld-advanced programm for SpinButton widget."""
  8     
  9     def __init__(self, title):
 10       self.window = gtk.Window()
 11       self.window.connect("destroy", lambda w: gtk.main_quit())
 12       self.window.set_title(title)
 13       self.window.set_size_request(200, 60)
 14       self.create_interior()   # "create" window content
 15       self.window.show_all()
 16     
 17     def create_interior(self):
 18       table = gtk.Table(2, 2, True)
 19       table.set_row_spacings(3)
 20       table.set_col_spacings(12)
 21       
 22       # <table row 1>
 23       # label for SpinButton
 24       label = gtk.Label("Value: ")
 25       table.attach(label, 0,1,0,1)
 26       # prepare SpinButton:
 27       # a) info about its range and behaviour are held by associated Adjustment (page_size attribute is not used)
 28       adj = gtk.Adjustment(value=0, lower=-10, upper=10, step_incr=0.1, page_incr=1)
 29       # b1) SpinButton's specific properties are part of its constructor
 30       spin_button = gtk.SpinButton(adj, climb_rate=0.01, digits=1)
 31       # b2) other SpinButton's specific properties can be set via dedicated setters
 32       spin_button.set_wrap(True)
 33       # attach SpinButton to display table
 34       table.attach(spin_button, 1,2,0,1)
 35   
 36       # <table row 2>
 37       # button for getting the value
 38       button = gtk.Button("value as Int")
 39       button.connect("clicked", self.get_value, spin_button)
 40       table.attach(button, 0,1,1,2)
 41       # label for output
 42       self.label = gtk.Label("")
 43       table.attach(self.label, 1,2,1,2)
 44       
 45       # append everything to window
 46       self.mainbox = table
 47       self.window.add(self.mainbox)
 48       self.mainbox.show()
 49     
 50     def get_value(self, button, spinButton):
 51       txt = "%d" % spinButton.get_value_as_int()
 52       self.label.set_text( txt )
 53     
 54     def main(self):
 55       gtk.main()
 56   
 57   
 58   if __name__ == "__main__":
 59     m = MyGUI("SpinButton example")
 60     m.main()
Screenshot:
Program screenshot gtk-spinbutton1-2.jpg
Doba běhu: 719.9 ms