Pygtk-spinbutton-class

提供:Dev Guides
移動先:案内検索

PyGTK-SpinButtonクラス

しばしばSpinnerと呼ばれるSpinnButtonウィジェットは、右に上下の矢印があるgtk.Entryウィジェットです。 ユーザーは、数値を直接入力するか、上下の矢印を使用して増減できます。 gtk.SpinButtonクラスは、gtk.Entryクラスから継承されます。 スピナーの数値の範囲とステップを制限できるgtk.Adjustmentオブジェクトを使用します。

SpinButtonウィジェットは、次のコンストラクタを使用して作成されます-

sp = gtk.SpinButton(adj, climb_rate, digits)

ここで、adjは範囲を制御する* gtk.Adjustmentオブジェクトを表し、 climb_rate は加速係数であり、数字で指定された小数の数です。

gtk.SpinButtonクラスには次のメソッドがあります-

  • SpinButton.set_adjustment()-これは「調整」プロパティを設定します。
  • SpinButton.set_digits()-これは、「digits」プロパティを値に設定して、スピンボタンによって表示される小数点以下の桁数を決定します。
  • SpinButton.set_increments(step、page)-左マウスボタンを押すたびに増分が適用されるステップ値と、中ボタンを押すたびに増分が適用されるページ値を設定します。
  • SpinButton.set_range()-これは、スピンボタンの最小値と最大値を設定します。
  • SpinButton.set_value()-これは、スピンボタンを新しい値にプログラムで設定します。
  • SpinButton.update_policy()-有効な値はgtk.UPDATE_ALWAYSとgtk.UPDATE_VALIDです
  • SpinButton.spin(direction、increment = 1)-これは、指定された方向にスピナーの値を増分または減分します。

以下は、事前定義された方向定数です-

gtk.SPIN_STEP_FORWARD forward by step_increment
gtk.SPIN_STEP_BACKWARD backward by step_increment
gtk.SPIN_PAGE_FORWARD forward by step_increment
gtk.SPIN_PAGE_BACKWARD backward by step_increment
gtk.SPIN_HOME move to minimum value
gtk.SPIN_END move to maximum value
gtk.SPIN_USER_DEFINED add increment to the value
*SpinButton.set_wrap()— wrapがTrueの場合、範囲の上限または下限を超えると、スピンボタンの値は反対の制限まで折り返します。

gtk.SpinButtonウィジェットは、次の信号を発します-

change-value This is emitted when the spinbutton value is changed by keyboard action
input This is emitted when the value changes.
output This is emitted when the spinbutton display value is changed. Returns* True* if the handler successfully sets the text and no further processing is required.
value-changed This is emitted when any of the settings that change the display of the spinbutton is changed.
wrapped This is emitted right after the spinbutton wraps from its maximum to minimum value or vice-versa.

次の例では、3つのSpinButtonウィジェットを使用して、単純な Date Selector を構築します。 Day Selectorは、1〜31の値を制限する調整オブジェクトに適用されます。 2番目のセレクターは、月1〜12の数用です。 3番目のセレクタは、2000〜2020年の範囲を選択します。

コードを観察します-

import gtk

class PyApp(gtk.Window):

   def __init__(self):
      super(PyApp, self).__init__()
      self.set_title("SpinButton Demo")
      self.set_size_request(300, 200)
      self.set_position(gtk.WIN_POS_CENTER)
      self.set_border_width(20)

      vbox = gtk.VBox(False, 5)
      hbox = gtk.HBox(True, 3)
      lbl1 = gtk.Label("Date")
      hbox.add(lbl1)

      adj1 = gtk.Adjustment(1.0, 1.0, 31.0, 1.0, 5.0, 0.0)
      spin1 = gtk.SpinButton(adj1, 0, 0)
      spin1.set_wrap(True)

      hbox.add(spin1)
      lbl2 = gtk.Label("Month")
      hbox.add(lbl2)

      adj2 = gtk.Adjustment(1.0, 1.0, 12.0, 1.0, 5.0, 0.0)
      spin2 = gtk.SpinButton(adj2, 0, 0)
      spin2.set_wrap(True)

      hbox.add(spin2)
      lbl3 = gtk.Label("Year")
      hbox.add(lbl3)

      adj3 = gtk.Adjustment(1.0, 2000.0, 2020.0, 1.0, 5.0, 0.0)
      spin3 = gtk.SpinButton(adj3, 0, 0)
      spin3.set_wrap(True)
      hbox.add(spin3)

      frame = gtk.Frame()
      frame.add(hbox)
      frame.set_label("Date of Birth")

      vbox.add(frame)
      self.add(vbox)
      self.connect("destroy", gtk.main_quit)
      self.show_all()
PyApp()
gtk.main()

実行すると、上記のコードは次の出力を生成します-

Spin​​Button Demo