目录

PyGTK - Box Class( Box Class)

gtk.Box类是一个抽象类,用于定义容器的功能,其中窗口小部件放置在矩形区域中。 gtk.HBox和gtk.VBox小部件都是从它派生的。

gtk.Hbox中的子窗口小部件水平排列在同一行中。 另一方面,gtk.VBox的子窗口小部件垂直排列在同一列中。

gtk.Box类使用以下构造函数 -

gtk.Box(homogenous = True, spacing = 0)

homogenous属性默认设置为True。 结果,所有子窗口小部件都被赋予相同的分配。

gtk.Box使用打包机制将子窗口小部件放在其中,引用特定位置,引用开始或结束。 pack_start()方法从头到尾放置小部件。 相反,pack_end()方法将小部件从头到尾放置。 或者,您可以使用类似于pack_start()的add()方法。

以下方法可用于gtk.HBox以及gtk.VBox -

  • gtk_box_pack_start ()

  • gtk_box_pack_end ()

gtk_box_pack_start ()

这个方法将child添加到框中,参考框的开头打包 -

pack_start(child, expand = True, fill = True, padding = 0)

以下是参数 -

  • child - 这是要添加到框中的窗口小部件对象

  • expand - 如果要在框中为子项提供额外空间,则将其设置为True。 所有子widgets之间都有额外的空间。

  • fill - 如果为True,将为子项分配额外的空间。 否则,此参数用作填充。

  • padding - 这是框中小部件之间的像素空间。

gtk_box_pack_end ()

这会将子项添加到框中,参考框的末尾打包。

pack_end (child, expand = True, fill = True, padding = 0)

以下是参数 -

  • child - 这是要添加的小部件对象

  • expand - 如果要在框中为子项提供额外空间,则将其设置为True。 这个额外的空间在所有子窗口小部件之间划分。

  • fill - 如果为True,则将额外空间分配给子项,否则将用作填充。

  • padding - 这是框中小部件之间的像素空间。

set_spacing (spacing)是设置放置在框的子项之间的像素数的函数。

add (widget)方法继承自gtk.Container类。 它将小部件添加到容器中。 可以使用此方法代替pack_start()方法。

例子 (Example)

在下面给出的示例中,顶层窗口包含一个垂直框(gtk.VBox对象框)。 它又有一个VBox对象vb和HBox对象hb。 在上方框中,标签,条目小部件和按钮垂直放置。 在下方框中,另一组标签,条目和按钮垂直放置。

请注意以下代码 -

import gtk
class PyApp(gtk.Window):
   def __init__(self):
      super(PyApp, self).__init__()
         self.set_title("Box demo")
      box = gtk.VBox()
      vb = gtk.VBox()
      lbl = gtk.Label("Enter name")
      vb.pack_start(lbl, expand = True, fill = True, padding = 10)
      text = gtk.Entry()
      vb.pack_start(text, expand = True, fill = True, padding = 10)
      btn = gtk.Button(stock = gtk.STOCK_OK)
      vb.pack_start(btn, expand = True, fill = True, padding = 10)
      hb = gtk.HBox()
      lbl1 = gtk.Label("Enter marks")
      hb.pack_start(lbl1, expand = True, fill = True, padding = 5)
      text1 = gtk.Entry()
      hb.pack_start(text1, expand = True, fill = True, padding = 5)
      btn1 = gtk.Button(stock = gtk.STOCK_SAVE)
      hb.pack_start(btn1, expand = True, fill = True, padding = 5)
      box.add(vb)
      box.add(hb)
      self.add(box)
      self.show_all()
PyApp()
gtk.main()

上面的代码将产生以下输出 -

盒子演示
↑回到顶部↑
WIKI教程 @2018