Pygtk-paned-class

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

PyGTK-ペインクラス

Panedクラスは、2つの調整可能なペインを水平(gtk.Hpaned)または垂直(gtk.Vpaned)に表示できるウィジェットの基本クラスです。 ペインに子ウィジェットを追加するには、pack1()およびpack2()メソッドを使用します。

パンウィジェットは、2つのペインの間にセパレータスライダーを描画し、相対的な幅/高さを調整するためのハンドルを提供します。 ペイン内の子ウィジェットのサイズ変更プロパティがTrueに設定されている場合、ペインのサイズに応じてサイズが変更されます。

次のメソッドは、HPanedおよびVPanedクラスで利用可能です-

  • Paned.add1(child)-これは、 child で指定されたウィジェットを上部または左ペインに追加します
  • Paned.add2(child)-これは、 child で指定されたウィジェットを下部または右側のペインに追加します。
  • Paned.pack1(child、resize、shrink)-これにより、 child で指定されたウィジェットがパラメータとともに上部または左側のペインに追加されます。 resizeTrue の場合、パンされたウィジェットのサイズが変更されたときに child のサイズを変更する必要があります。 shrinkTrue の場合、 child は最小サイズのリクエストよりも小さくすることができます。 *Paned.pack2(child、resize、shrink)-これは、2つのペイン間の仕切りの位置を設定します。

Panedウィジェットの両方のタイプは、次の信号を発します-

accept-position This is emitted when* paned *has the focus causing the child widget with the focus to be activated.
cancel-position This is emitted when the* Esc key is pressed while paned *has the focus.
move-handle This is emitted when* paned *has the focus and the separator is moved.

次の例では、gtk.Hpanedウィジェットを使用しています。 左ペインにTreeViewウィジェットが追加され、右ペインにTextViewウィジェットが追加されます。 TreeViewのいずれかの行が選択されると、コールバック関数に接続されているrow_activated信号を発します。* on_activated()function *は、行のテキストを取得し、テキストビューパネルに表示します。

コードを観察します-

import gtk, gobject

class PyApp(gtk.Window):
   def __init__(self):
      super(PyApp, self).__init__()
      self.set_title("HPaned widget Demo")
      self.set_default_size(250, 200)
      vp = gtk.HPaned()
      sw = gtk.ScrolledWindow()
      sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)

      tree = gtk.TreeView()
      languages = gtk.TreeViewColumn()
      languages.set_title("GUI Toolkits")
      cell = gtk.CellRendererText()
      languages.pack_start(cell, True)
      languages.add_attribute(cell, "text", 0)
      treestore = gtk.TreeStore(str)
      it = treestore.append(None, ["Python"])

      treestore.append(it, ["PyQt"])
      treestore.append(it, ["wxPython"])
      treestore.append(it, ["PyGTK"])
      treestore.append(it, ["Pydide"])

      it = treestore.append(None, ["Java"])
      treestore.append(it, ["AWT"])
      treestore.append(it, ["Swing"])
      treestore.append(it, ["JSF"])
      treestore.append(it, ["SWT"])

      tree.append_column(languages)
      tree.set_model(treestore)

      vp.add1(tree)
      self.tv = gtk.TextView()
      vp.add2(self.tv)
      vp.set_position(100)
      self.add(vp)

      tree.connect("row-activated", self.on_activated)
      self.connect("destroy", gtk.main_quit)
      self.show_all()

   def on_activated(self, widget, row, col):
      model = widget.get_model()
      text = model[row][0]
      print text

      buffer = gtk.TextBuffer()
      buffer.set_text(text+" is selected")
      self.tv.set_buffer(buffer)

if __name__ == '__main__':
   PyApp()
   gtk.main()

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

Hpaned Widget Demo