PyGobject(五十)布局容器之ListBox
2016-07-30 13:02
579 查看
GtkListBox
继承关系
Methods
Virtual Methods
Properties
Signals
例子
附录
GtkSelectionMode
Gtk. ListBox经常用来替代Gtk.TreeView,特别是当列表内容包含有其他复杂的布局,或者是列表内容是可互动的(如按钮)
虽然Gtk.ListBox只能包含Gtk.ListBoxRow孩子,但是可以通过Gtk.Container.add()添加任何小部件到其中,Gtk.ListBoxRow会自动包裹部件并插入到Gtk.ListBox中。
![](https://oscdn.geek-share.com/Uploads/Images/Content/202010/31/b68a66bcd80e2923c0417ec6c5d246d5)
![](https://oscdn.geek-share.com/Uploads/Images/Content/202010/31/9a22dc3e187dd6834982b8c234f8d974)
代码:
代码解析
先创建一个Gtk.Box,并添加到当前窗口
创建第一个Gtk.ListBox,设置选择模式为不可选,并添加到前面创建的box_outer中
创建第一行Gtk.ListBoxRow,并向其中添加一个水平box
水平box中添加一个垂直的box和一个Gtk.Switch,垂直的box中添加了两个Label
将这一行添加到listbox中
接下来的两行内容Gtk.ListBoxRow跳过
接着创建了第二个Gtk.ListBox
设置每行内容
设置排序方法,按字母排序
设置过滤方法,过滤掉“Fail”项
连接’row-activated’信号,点击行时,输出行内容
Bases: GObject.GEnum
NONE = 0
不可选择
SINGLE = 1
0或1个元素可被选择
BROWSE = 2
必须有一个被选择
MULTIPLE = 3
多选。可按Ctrl键或Shift键
代码下载地址:http://download.csdn.net/detail/a87b01c14/9594728
继承关系
Methods
Virtual Methods
Properties
Signals
例子
附录
GtkSelectionMode
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.SelectionModeBases: GObject.GEnum
NONE = 0
不可选择
SINGLE = 1
0或1个元素可被选择
BROWSE = 2
必须有一个被选择
MULTIPLE = 3
多选。可按Ctrl键或Shift键
代码下载地址:http://download.csdn.net/detail/a87b01c14/9594728
相关文章推荐
- PyGobject(四)布局容器概述
- PyGobject(十)布局容器之Button篇——Gtk.CheckButton
- PyGobject(十七)布局容器之Button篇——Gtk.ModelButton
- PyGobject(二十五)布局容器之Overlay
- PyGobject(三十八)布局容器之ScrolledWindow
- PyGobject(四十二)布局容器之ButtonBox
- PyGobject(五)布局容器之Button篇——Gtk.Button
- PyGobject(二十九)布局容器之SearchBar
- PyGobject(三十四)布局容器之Assistant
- PyGobject(五十六)布局容器之TreeView(上)
- PyGobject(三十九)布局容器之PlacesSidebar
- PyGobject(十八)布局容器之ActionBar
- PyGobject(三十)布局容器之StackSidebar
- PyGobject(三十五)布局容器之ShortcutsWindow
- PyGobject(四十三)布局容器之InfoBar
- PyGobject(十一)布局容器之Button篇——Gtk.RadioButton
- PyGobject(四十四)布局容器之Stack&StackSwitcher
- PyGobject(四十五)布局容器之Grid
- PyGobject(十二)布局容器之Button篇——Gtk.MenuButton
- PyGobject(四十六)布局容器之Fixed