PyGobject(五十)布局容器之ListBox
Gtk.ListBox是一个垂直容器,只能包含Gtk.ListBoxRow行对象,但是Gtk.ListBoxRow可以包含多个任何部件,可以根据行内容来排序和过滤。并且可以使用键盘和鼠标来导航和选择行。Gtk. ListBox经常用来替代Gtk.TreeView,特别是当列表内容包含有其他复杂的布局,或者是列表内容是可互动的(如按钮)
Gtk.ListBox
Gtk.ListBox是一个垂直容器,只能包含Gtk.ListBoxRow行对象,但是Gtk.ListBoxRow可以包含多个任何部件,可以根据行内容来排序和过滤。并且可以使用键盘和鼠标来导航和选择行。
Gtk. ListBox经常用来替代Gtk.TreeView,特别是当列表内容包含有其他复杂的布局,或者是列表内容是可互动的(如按钮)
虽然Gtk.ListBox只能包含Gtk.ListBoxRow孩子,但是可以通过Gtk.Container.add()添加任何小部件到其中,Gtk.ListBoxRow会自动包裹部件并插入到Gtk.ListBox中。
继承关系
Gtk.ListBox是Gtk.Container的直接子类
Methods
方法修饰词 | 方法名及参数 |
---|---|
static | new () |
bind_model (model, create_widget_func, *user_data) | |
drag_highlight_row (row) | |
drag_unhighlight_row () | |
get_activate_on_single_click () | |
get_adjustment () | |
get_row_at_index (index_) | |
get_row_at_y (y) | |
get_selected_row () | |
get_selected_rows () | |
get_selection_mode () | |
insert (child, position) | |
invalidate_filter () | |
invalidate_headers () | |
invalidate_sort () | |
prepend (child) | |
select_all () | |
select_row (row) | |
selected_foreach (func, *data) | |
set_activate_on_single_click (single) | |
set_adjustment (adjustment) | |
set_filter_func (filter_func, *user_data) | |
set_header_func (update_header, *user_data) | |
set_placeholder (placeholder) | |
set_selection_mode (mode) | |
set_sort_func (sort_func, *user_data) | |
unselect_all () | |
unselect_row (row) |
Virtual Methods
do_activate_cursor_row () |
do_move_cursor (step, count) |
do_row_activated (row) |
do_row_selected (row) |
do_select_all () |
do_selected_rows_changed () |
do_toggle_cursor_row () |
do_unselect_all () |
Properties
Name | Type | Flags | Short Description |
---|---|---|---|
activate-on-single-click | bool | r/w/en | 通过单击来激活行 |
selection-mode | Gtk.SelectionMode | r/w/en | 选择模式 |
Signals
Name | Short Description |
---|---|
activate-cursor-row | |
move-cursor | |
row-activated | The ::row-activated signal is emitted when a row has been activated by the user. |
row-selected | The ::row-selected signal is emitted when a new row is selected, or (with a None row) when the selection is cleared. |
select-all | The ::select-all signal is a keybinding signal which gets emitted to select all children of the box, if the selection mode permits it. |
selected-rows-changed | The ::selected-rows-changed signal is emitted when the set of selected rows changes. |
toggle-cursor-row | |
unselect-all | The ::unselect-all signal is a keybinding signal which gets emitted to unselect all children of the box, if the selection mode permits it. |
例子
代码:
#!/usr/bin/env python3
# Created by xiaosanyu at 16/6/13
# section 067
TITLE = "ListBox"
DESCRIPTION = """
A Gtk.ListBox is a vertical container that contains Gtk.ListBoxRow children.
These rows can by dynamically sorted and filtered, and headers can be added dynamically
depending on the row content.
It also allows keyboard and mouse navigation and selection like a typical list.
"""
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class ListBoxRowWithData(Gtk.ListBoxRow):
def __init__(self, data):
super(Gtk.ListBoxRow, self).__init__()
self.data = data
self.add(Gtk.Label(data))
class ListBoxWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="ListBox Demo")
self.set_border_width(10)
box_outer = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
self.add(box_outer)
listbox = Gtk.ListBox()
listbox.set_selection_mode(Gtk.SelectionMode.NONE)
box_outer.pack_start(listbox, True, True, 0)
row = Gtk.ListBoxRow()
hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=50)
row.add(hbox)
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
hbox.pack_start(vbox, True, True, 0)
label1 = Gtk.Label(label="Automatic Date & Time", xalign=0)
label2 = Gtk.Label(label="Requires internet access", xalign=0)
vbox.pack_start(label1, True, True, 0)
vbox.pack_start(label2, True, True, 0)
switch = Gtk.Switch()
switch.props.valign = Gtk.Align.CENTER
hbox.pack_start(switch, False, True, 0)
listbox.add(row)
row = Gtk.ListBoxRow()
hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=50)
row.add(hbox)
label = Gtk.Label(label="Enable Automatic Update", xalign=0)
check = Gtk.CheckButton()
hbox.pack_start(label, True, True, 0)
hbox.pack_start(check, False, True, 0)
listbox.add(row)
row = Gtk.ListBoxRow()
hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=50)
row.add(hbox)
label = Gtk.Label(label="Date Format", xalign=0)
combo = Gtk.ComboBoxText()
combo.insert(0, "0", "24-hour")
combo.insert(1, "1", "AM/PM")
hbox.pack_start(label, True, True, 0)
hbox.pack_start(combo, False, True, 0)
listbox.add(row)
listbox_2 = Gtk.ListBox()
items = 'This is a sorted ListBox Fail'.split()
for item in items:
listbox_2.add(ListBoxRowWithData(item))
def sort_func(row_1, row_2, data, notify_destroy):
return row_1.data.lower() > row_2.data.lower()
def filter_func(row, data, notify_destroy):
return False if row.data == 'Fail' else True
listbox_2.set_sort_func(sort_func, None, False)
listbox_2.set_filter_func(filter_func, None, False)
listbox_2.connect('row-activated', lambda widget, row: print(row.data))
box_outer.pack_start(listbox_2, True, True, 0)
listbox_2.show_all()
def main():
win = ListBoxWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
if __name__ == "__main__":
main()
代码解析
box_outer = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
self.add(box_outer)
先创建一个Gtk.Box,并添加到当前窗口
listbox = Gtk.ListBox() listbox.set_selection_mode(Gtk.SelectionMode.NONE)
box_outer.pack_start(listbox, True, True, 0)
创建第一个Gtk.ListBox,设置选择模式为不可选,并添加到前面创建的box_outer中
row = Gtk.ListBoxRow()
hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=50)
row.add(hbox)
创建第一行Gtk.ListBoxRow,并向其中添加一个水平box
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
hbox.pack_start(vbox, True, True, 0)
label1 = Gtk.Label(label="Automatic Date & Time", xalign=0)
label2 = Gtk.Label(label="Requires internet access", xalign=0)
vbox.pack_start(label1, True, True, 0)
vbox.pack_start(label2, True, True, 0)
switch = Gtk.Switch()
switch.props.valign = Gtk.Align.CENTER
hbox.pack_start(switch, False, True, 0)
水平box中添加一个垂直的box和一个Gtk.Switch,垂直的box中添加了两个Label
listbox.add(row)
将这一行添加到listbox中
接下来的两行内容Gtk.ListBoxRow跳过
接着创建了第二个Gtk.ListBox
listbox_2 = Gtk.ListBox()
设置每行内容
items = 'This is a sorted ListBox Fail'.split()
for item in items:
listbox_2.add(ListBoxRowWithData(item))
class ListBoxRowWithData(Gtk.ListBoxRow):
def __init__(self, data):
super(Gtk.ListBoxRow, self).__init__()
self.data = data
self.add(Gtk.Label(data))
设置排序方法,按字母排序
listbox_2.set_sort_func(sort_func, None, False)
def sort_func(row_1, row_2, data, notify_destroy):
return row_1.data.lower() > row_2.data.lower()
设置过滤方法,过滤掉“Fail”项
listbox_2.set_filter_func(filter_func, None, False)
def filter_func(row, data, notify_destroy):
return False if row.data == 'Fail' else True
连接’row-activated’信号,点击行时,输出行内容
listbox_2.connect('row-activated', lambda widget, row: print(row.data))
附录
Gtk.SelectionMode
class Gtk.SelectionMode
Bases: GObject.GEnum
NONE = 0
不可选择
SINGLE = 1
0或1个元素可被选择
BROWSE = 2
必须有一个被选择
MULTIPLE = 3
多选。可按Ctrl键或Shift键
更多推荐
所有评论(0)