PyGobject(五十八)布局容器之IconView
来源:互联网 发布:lte中d2怎么优化 编辑:程序博客网 时间:2024/05/01 00:06
- GtkIconView
- 继承关系
- Methods
- Virtual Methods
- Properties
- Signals
- 例子
- 一
- 二
- 三
Gtk.IconView
Gtk.IconView提供基于Gtk.TreeModel的另一种视图。它显示一个带文本和图标的网格视图。像Gtk.TreeView一样,允许单选和多选列表行。可以使用方向键选择,还支持使用鼠标进行拖放
继承关系
Gtk.IconView是Gtk.Container的直接子类
Methods
Virtual Methods
Properties
Signals
例子
一
代码:
#!/usr/bin/env python3# Created by xiaosanyu at 16/6/16# section 088TITLE = "IconView"DESCRIPTION = """Gtk.IconView provides an alternative view on a Gtk.TreeModel.It displays the model as a grid of icons with labels. Like Gtk.TreeView"""import gigi.require_version('Gtk', '3.0')from gi.repository import Gtkfrom gi.repository.GdkPixbuf import Pixbuficons = ["edit-cut", "edit-paste", "edit-copy"]class IconViewWindow(Gtk.Window): def __init__(self): Gtk.Window.__init__(self) self.set_default_size(200, 200) liststore = Gtk.ListStore(Pixbuf, str) iconview = Gtk.IconView.new() iconview.set_model(liststore) iconview.set_pixbuf_column(0) iconview.set_text_column(1) theme = Gtk.IconTheme.get_default() # print(theme.get_search_path()) for icon in icons: if theme.has_icon(icon): pixbuf = theme.load_icon(icon, 48, 0) liststore.append([pixbuf, "Label"]) self.add(iconview)def main(): win = IconViewWindow() win.connect("delete-event", Gtk.main_quit) win.show_all() Gtk.main()if __name__ == "__main__": main()
代码解析:
liststore = Gtk.ListStore(Pixbuf, str)
创建一个Gtk.ListStore,第一列为图片,第二列为文本
iconview = Gtk.IconView.new()iconview.set_model(liststore)iconview.set_pixbuf_column(0)iconview.set_text_column(1)
创建Gtk.IconView,设置model为ListStore,设置图片列为第一列,文本列为第二列
theme = Gtk.IconTheme.get_default() # print(theme.get_search_path()) for icon in icons: if theme.has_icon(icon): pixbuf = theme.load_icon(icon, 48, 0) liststore.append([pixbuf, "Label"])
填充liststore
二
代码
#!/usr/bin/env python3# section 089# -*- Mode: Python; py-indent-offset: 4 -*-# vim: tabstop=4 shiftwidth=4 expandtab## Copyright (C) 2010 Red Hat, Inc., John (J5) Palmieri <johnp@redhat.com>## This library is free software; you can redistribute it and/or# modify it under the terms of the GNU Lesser General Public# License as published by the Free Software Foundation; either# version 2.1 of the License, or (at your option) any later version.## This library is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU# Lesser General Public License for more details.## You should have received a copy of the GNU Lesser General Public# License along with this library; if not, write to the Free Software# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301# USATITLE = "Icon View Basics"DESCRIPTION = """The GtkIconView widget is used to display and manipulateicons. It uses a GtkTreeModel for data storage, so the list store example mightbe helpful. We also use the Gio.File API to get the icons for each file type."""import osimport gigi.require_version('GdkPixbuf', '2.0')gi.require_version('Gtk', '3.0')from gi.repository import GLib, Gio, GdkPixbuf, Gtkclass IconViewApp: (COL_PATH, COL_DISPLAY_NAME, COL_PIXBUF, COL_IS_DIRECTORY, NUM_COLS) = range(5) def __init__(self): self.pixbuf_lookup = {} self.window = Gtk.Window() self.window.set_title('Gtk.IconView demo') self.window.set_default_size(650, 400) self.window.connect('destroy', Gtk.main_quit) vbox = Gtk.VBox() self.window.add(vbox) tool_bar = Gtk.Toolbar() vbox.pack_start(tool_bar, False, False, 0) up_button = Gtk.ToolButton(stock_id=Gtk.STOCK_GO_UP) up_button.set_is_important(True) up_button.set_sensitive(False) tool_bar.insert(up_button, -1) home_button = Gtk.ToolButton(stock_id=Gtk.STOCK_HOME) home_button.set_is_important(True) tool_bar.insert(home_button, -1) sw = Gtk.ScrolledWindow() sw.set_shadow_type(Gtk.ShadowType.ETCHED_IN) sw.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC) vbox.pack_start(sw, True, True, 0) # create the store and fill it with content self.parent_dir = '/' store = self.create_store() self.fill_store(store) icon_view = Gtk.IconView(model=store) icon_view.set_selection_mode(Gtk.SelectionMode.MULTIPLE) sw.add(icon_view) # connect to the 'clicked' signal of the "Up" tool button up_button.connect('clicked', self.up_clicked, store) # connect to the 'clicked' signal of the "home" tool button home_button.connect('clicked', self.home_clicked, store) self.up_button = up_button self.home_button = home_button # we now set which model columns that correspond to the text # and pixbuf of each item icon_view.set_text_column(self.COL_DISPLAY_NAME) icon_view.set_pixbuf_column(self.COL_PIXBUF) # connect to the "item-activated" signal icon_view.connect('item-activated', self.item_activated, store) icon_view.grab_focus() self.window.show_all() def sort_func(self, store, a_iter, b_iter, user_data): (a_name, a_is_dir) = store.get(a_iter, self.COL_DISPLAY_NAME, self.COL_IS_DIRECTORY) (b_name, b_is_dir) = store.get(b_iter, self.COL_DISPLAY_NAME, self.COL_IS_DIRECTORY) if a_name is None: a_name = '' if b_name is None: b_name = '' if (not a_is_dir) and b_is_dir: return 1 elif a_is_dir and (not b_is_dir): return -1 elif a_name > b_name: return 1 elif a_name < b_name: return -1 else: return 0 def up_clicked(self, item, store): self.parent_dir = os.path.split(self.parent_dir)[0] self.fill_store(store) # de-sensitize the up button if we are at the root self.up_button.set_sensitive(self.parent_dir != '/') def home_clicked(self, item, store): self.parent_dir = GLib.get_home_dir() self.fill_store(store) # Sensitize the up button self.up_button.set_sensitive(True) def item_activated(self, icon_view, tree_path, store): iter_ = store.get_iter(tree_path) (path, is_dir) = store.get(iter_, self.COL_PATH, self.COL_IS_DIRECTORY) if not is_dir: return self.parent_dir = path self.fill_store(store) self.up_button.set_sensitive(True) def create_store(self): store = Gtk.ListStore(str, str, GdkPixbuf.Pixbuf, bool) # set sort column and function store.set_default_sort_func(self.sort_func) store.set_sort_column_id(-1, Gtk.SortType.ASCENDING) return store def file_to_icon_pixbuf(self, path): pixbuf = None # get the theme icon f = Gio.file_new_for_path(path) info = f.query_info(Gio.FILE_ATTRIBUTE_STANDARD_ICON, Gio.FileQueryInfoFlags.NONE, None) gicon = info.get_icon() # check to see if it is an image format we support for format in GdkPixbuf.Pixbuf.get_formats(): for mime_type in format.get_mime_types(): content_type = Gio.content_type_from_mime_type(mime_type) if content_type is not None: break format_gicon = Gio.content_type_get_icon(content_type) if format_gicon.equal(gicon): #gicon = f.icon_new() break if gicon in self.pixbuf_lookup: return self.pixbuf_lookup[gicon] if isinstance(gicon, Gio.ThemedIcon): names = gicon.get_names() icon_theme = Gtk.IconTheme.get_default() for name in names: try: pixbuf = icon_theme.load_icon(name, 64, 0) break except GLib.GError: pass self.pixbuf_lookup[gicon] = pixbuf elif isinstance(gicon, Gio.FileIcon): icon_file = gicon.get_file() path = icon_file.get_path() pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(path, 72, 72) self.pixbuf_lookup[gicon] = pixbuf return pixbuf def fill_store(self, store): store.clear() for name in os.listdir(self.parent_dir): path = os.path.join(self.parent_dir, name) is_dir = os.path.isdir(path) pixbuf = self.file_to_icon_pixbuf(path) store.append((path, name, pixbuf, is_dir))def main(): IconViewApp() Gtk.main()if __name__ == '__main__': main()
三
代码
#!/usr/bin/env python3# section 090# -*- Mode: Python; py-indent-offset: 4 -*-# vim: tabstop=4 shiftwidth=4 expandtab## Copyright (C) 2010 Red Hat, Inc., John (J5) Palmieri <johnp@redhat.com>## This library is free software; you can redistribute it and/or# modify it under the terms of the GNU Lesser General Public# License as published by the Free Software Foundation; either# version 2.1 of the License, or (at your option) any later version.## This library is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU# Lesser General Public License for more details.## You should have received a copy of the GNU Lesser General Public# License along with this library; if not, write to the Free Software# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301# USATITLE = "Editing and Drag-and-Drop"DESCRIPTION = """The GtkIconView widget supports Editing and Drag-and-Drop.This example also demonstrates using the generic GtkCellLayout interface to setup cell renderers in an icon view."""import gigi.require_version('Gtk', '3.0')from gi.repository import Gtk, Gdk, GdkPixbufclass IconviewEditApp: COL_TEXT = 0 NUM_COLS = 1 def __init__(self): self.window = Gtk.Window() self.window.set_title('Editing and Drag-and-Drop') self.window.set_border_width(8) self.window.connect('destroy', Gtk.main_quit) store = Gtk.ListStore(str) colors = ['Red', 'Green', 'Blue', 'Yellow'] store.clear() for c in colors: store.append([c]) icon_view = Gtk.IconView(model=store) icon_view.set_selection_mode(Gtk.SelectionMode.SINGLE) icon_view.set_item_orientation(Gtk.Orientation.HORIZONTAL) icon_view.set_columns(2) icon_view.set_reorderable(True) renderer = Gtk.CellRendererPixbuf() icon_view.pack_start(renderer, True) icon_view.set_cell_data_func(renderer, self.set_cell_color, None) renderer = Gtk.CellRendererText() icon_view.pack_start(renderer, True) renderer.props.editable = True renderer.connect('edited', self.edited, icon_view) icon_view.add_attribute(renderer, 'text', self.COL_TEXT) self.window.add(icon_view) self.window.show_all() def set_cell_color(self, cell_layout, cell, tree_model, iter_, icon_view): # FIXME return single element instead of tuple text = tree_model.get(iter_, self.COL_TEXT)[0] color = Gdk.color_parse(text) pixel = 0 if color is not None: pixel = ((color.red >> 8) << 24 | (color.green >> 8) << 16 | (color.blue >> 8) << 8) pixbuf = GdkPixbuf.Pixbuf.new(GdkPixbuf.Colorspace.RGB, False, 8, 24, 24) pixbuf.fill(pixel) cell.props.pixbuf = pixbuf def edited(self, cell, path_string, text, icon_view): model = icon_view.get_model() path = Gtk.TreePath(path_string) iter_ = model.get_iter(path) model.set_row(iter_, [text])def main(): IconviewEditApp() Gtk.main()if __name__ == '__main__': main()
代码下载地址:http://download.csdn.net/detail/a87b01c14/9594728
0 0
- PyGobject(五十八)布局容器之IconView
- PyGobject(十八)布局容器之ActionBar
- PyGobject(十九)布局容器之Alignment
- PyGobject(三十)布局容器之StackSidebar
- PyGobject(三十一)布局容器之EventBox
- PyGobject(三十二)布局容器之Window
- PyGobject(三十三)布局容器之ApplicationWindow
- PyGobject(三十四)布局容器之Assistant
- PyGobject(三十五)布局容器之ShortcutsWindow
- PyGobject(三十六)布局容器之OffscreenWindow
- PyGobject(三十七)布局容器之Dialog
- PyGobject(三十八)布局容器之ScrolledWindow
- PyGobject(三十九)布局容器之PlacesSidebar
- PyGobject(四十)布局容器之Viewport
- PyGobject(四十一)布局容器之Box
- PyGobject(四十二)布局容器之ButtonBox
- PyGobject(四十五)布局容器之Grid
- PyGobject(四十六)布局容器之Fixed
- Popup组件的属性ContentDelivery各个值的的含义
- ubuntu安装node的问题
- 关于堆内存,栈内存,方法区的解析
- POJ 3083 Children of the Candy Corn (DFS+BFS)
- Python判断一个字符串是否包含子串的几种方法
- PyGobject(五十八)布局容器之IconView
- HDU2177:取(2堆)石子游戏(威佐夫博弈)
- this关键字用法
- 判断集合是否为空
- 让ie浏览器成为支持html5的浏览器的解决方法(使用html5shiv)
- 【Linux全面学习】13.使用vsftp在不同系统间共享文件
- 如何向html表格中输入文本
- 缓动类算法
- 数据库