這篇文章講網(wǎng)格
網(wǎng)格應(yīng)該很好理解, 就是這樣的
Grid
然后添加控件, 并為控件分布格子的大小
主要繼承關(guān)系 Gtk.Grid < Gtk.Container < Gtk.Widget
如何創(chuàng)建一個(gè)Grid容器
通過實(shí)例化Gtk.Grid來創(chuàng)建一個(gè)Grid self.grid = Gtk.Grid()
Grid的使用
需要用到attach(child, left, top, width, height)
方法來放置控件, 有時(shí)會(huì)用到attach_next_to(child, sibling, position, width, height)
方法
- attach(child, left, top, width, height)
- child 子控件
- left, top 相當(dāng)與平面直角坐標(biāo)系, 以左上角為原點(diǎn), 向右為x正方向, 向下為Y正方向, left就是X, top就是Y, 具體看下圖
- width, height 子控件的寬和高
Grid
- attach_next_to(child, sibling, position, width, height)
- sibling 和 child, 這么說吧, 如果現(xiàn)在有一個(gè)按鈕控件在grid里, 你想往里面放一個(gè)標(biāo)簽控件, 想把這個(gè)放到按鈕的右邊, 那么child就是你準(zhǔn)備放的控件, sibling就是基于的控件, 比如在這里基于按鈕, 放在右邊 就可以寫attach_next_to(self.label, self.button, Gtk.PositionType.RIGHT, width, height)
- position 方向, 接受Gtk.PositionType
Gtk.PositionType 枚舉類型
Gtk.Position.LEFT = 0 左
Gtk.Position.RIGHT = 1 右
Gtk.Position.TOP = 2 上
Gtk.Position.BOTTON = 3 下
示例
-
3 x 3
Example -
原始碼實(shí)現(xiàn)
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
class MyWindow(Gtk.Window):
def __init__(self):
super(MyWindow, self).__init__(title="Hello")
self.grid = Gtk.Grid()
self.add(self.grid)
self.btn1 = Gtk.Button(label="Button 1")
self.btn2 = Gtk.Button(label="Button 2")
self.btn3 = Gtk.Button(label="Button 3")
self.label = Gtk.Label(label="Im a label")
self.grid.attach(self.btn1, 0, 0, 2, 1)
self.grid.attach_next_to(self.btn2, self.btn1, Gtk.PositionType.BOTTOM, 2, 1)
self.grid.attach_next_to(self.label, self.btn1, Gtk.PositionType.RIGHT, 1, 2)
self.grid.attach_next_to(self.btn3, self.btn2, Gtk.PositionType.BOTTOM, 3, 1)
win = MyWindow()
win.show_all()
win.connect("destroy", Gtk.main_quit)
Gtk.main()
Run
Grid的常用屬性
- column_homogeneous 列是否等寬
- row_homogeneous 行是否等高
- column_spacing 列之間間隔
- row_spacing 行之間間隔
- border_width 邊框大小
常用方法
- insert_column(int) 插入列, int是第幾列
- remove_column(int) 移除列, int是第幾列
- insert_row(int) 插入行, int是第幾行
- remove_column(int) 移除行, int是第幾行
- set_column_homogeneous(bool) 不用說了吧, 就是設(shè)置屬性的, 把set變?yōu)間et表示獲取該屬性
- set_row_homogeneous(bool)
- set_row_spacing(int)
- set_column_spacing(int)
- get_child_at(left, top) 根據(jù)left, top值獲取該位置上的控件
常用信號(hào)
- 參考上一篇文章(都是重復(fù)的)
之后 可能 會(huì)講一個(gè)簡單的計(jì)算器的實(shí)現(xiàn)
歡迎留言, 下一篇文章講解notebook 容器