📜  PyGTK-窗格类

📅  最后修改于: 2020-11-08 08:00:13             🧑  作者: Mango


Paned类是窗口小部件的基类,它可以水平(gtk.Hpaned)或垂直(gtk.Vpaned)显示两个可调窗格。窗格的子窗口小部件通过使用pack1()和pack2()方法添加。

“平移”窗口小部件在两个窗格之间绘制一个分隔器滑块,并提供一个调整其相对宽度/高度的手柄。如果窗格内子窗口小部件的resize属性设置为True,它将根据窗格的大小进行调整。

以下方法可用于HPaned以及VPaned类-

  • Paned.add1(child)-这会将child指定的小部件添加到顶部或左侧窗格中

  • Paned.add2(child)-这会将child指定的小部件添加到底部或右侧窗格中。

  • Paned.pack1(child,resize,收缩)-这会将child指定的小部件添加到带有参数的顶部或左侧窗格中。如果resizeTrue ,则在调整窗格小部件的大小时应调整child的大小。如果收缩True ,则可以使child小于其最小大小要求。

  • Paned.pack2(child,resize,收缩)-这设置了两个窗格之间分隔线的位置。

两种类型的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()函数检索行的文本并显示在文本视图面板中。

遵守代码-

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演示