Python – GTK+ 3 中的网格容器
Gtk.Grid
是一个容器,它在行和列中排列其子小部件,而没有在构造函数中指定尺寸。使用Gtk.Grid.attac
h() 添加子级。它们可以跨越多行或多列。
The Gtk.Grid.attach()
method takes five parameters:
child
: the Gtk.Widget
to add.
left
: the column number to attach the left side of child
to.
top
: indicates the row number to attach the top side of child
to.
width
: indicates the number of columns that the child
will span.
height
: indicates the number of rows that the child
will span.
使用Gtk.Grid.attach_next_to()
也可以在现有孩子旁边添加一个孩子。
The Gtk.Grid.attach_next_to
method takes five parameters:
child
:Gtk.Widget to add.
sibling
: an existing child
widget of a Gtk.Grid
or None. The child widget will be placed next to sibling
.
side
: Gtk.PositionType
indicating the side of sibling.
width
: indicate the number of columns the child
widget will span.
height
: indicate the number of rows the child
widget will span
请按照以下步骤操作:
- 导入 GTK+ 3 模块。
- 创建主窗口。
- 创建按钮。
- 创建网格。
注意:在像 Pycharm 这样的 IDE 中,我们可以安装一个名为 PyGObject 的包来使用 GTK+ 3。
import gi
# Since a system can have multiple versions
# of GTK + installed, we want to make
# sure that we are importing GTK + 3.
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
class GridWin(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title ="GfG")
grid = Gtk.Grid()
self.add(grid)
button1 = Gtk.Button(label ="Button 1")
button2 = Gtk.Button(label ="Button 2")
button3 = Gtk.Button(label ="Button 3")
button4 = Gtk.Button(label ="Button 4")
button5 = Gtk.Button(label ="Button 5")
button6 = Gtk.Button(label ="Button 6")
grid.add(button1)
# With in parentheses child, left, top, width,
# height respectively
grid.attach(button2, 1, 0, 2, 1)
# With in parentheses child, sibling, left, top, width,
# height respectively
grid.attach_next_to(button3, button1, Gtk.PositionType.BOTTOM, 1, 2)
grid.attach_next_to(button4, button3, Gtk.PositionType.RIGHT, 1, 1)
grid.attach(button5, 1, 2, 1, 1)
grid.attach_next_to(button6, button4, Gtk.PositionType.RIGHT, 1, 2)
win = GridWin()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
输出 :
在评论中写代码?请使用 ide.geeksforgeeks.org,生成链接并在此处分享链接。