我是靠谱客的博主 平淡八宝粥,最近开发中收集的这篇文章主要介绍PyGobject(四十七)布局容器之FlowBoxGtk.FlowBox附录,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

  • GtkFlowBox
    • 继承关系
    • Methods
    • Virtual Methods
    • Properties
    • Signals
    • 例子
  • 附录
    • GtkSelectionMode

Gtk.FlowBox

Gtk.FlowBox流式布局。根据窗口大小,自动调整子部件所在的位置。

继承关系

Gtk.FlowBox是Gtk.Container的直接子类
这里写图片描述

Methods

方法修饰词方法名及参数
staticnew ()
bind_model (model, create_widget_func, *user_data)
get_activate_on_single_click ()
get_child_at_index (idx)
get_column_spacing ()
get_homogeneous ()
get_max_children_per_line ()
get_min_children_per_line ()
get_row_spacing ()
get_selected_children ()
get_selection_mode ()
insert (widget, position)
invalidate_filter ()
invalidate_sort ()
select_all ()
select_child (child)
selected_foreach (func, *data)
set_activate_on_single_click (single)
set_column_spacing (spacing)
set_filter_func (filter_func, *user_data)
set_hadjustment (adjustment)
set_homogeneous (homogeneous)
set_max_children_per_line (n_children)
set_min_children_per_line (n_children)
set_row_spacing (spacing)
set_selection_mode (mode)
set_sort_func (sort_func, *user_data)
set_vadjustment (adjustment)
unselect_all ()
unselect_child (child)

Virtual Methods

do_activate_cursor_child ()
do_child_activated (child)
do_move_cursor (step, count)
do_select_all ()
do_selected_children_changed ()
do_toggle_cursor_child ()
do_unselect_all ()

Properties

NameTypeFlagsShort Description
activate-on-single-clickboolr/w/en单击是否激活子部件
column-spacingintr/w/en列间距
homogeneousboolr/w/en孩子部件是否拥有相同大小
max-children-per-lineintr/w/en每行最大孩子个数
min-children-per-lineintr/w/en每行最小孩子个数
row-spacingintr/w/en行间距
selection-modeGtk.SelectionModer/w/en选择模式

Signals

NameShort Description
activate-cursor-childThe ::activate-cursor-child signal is a keybinding signal which gets emitted when the user activates the box.
child-activated子部件被选中发送此信号.
move-cursorThe ::move-cursor signal is a keybinding signal which gets emitted when the user initiates a cursor movement.
select-all选中全部子部件发送此信号.
selected-children-changed子部件选择情况改变
toggle-cursor-childThe ::toggle-cursor-child signal is a keybinding signal which toggles the selection of the child that has the focus.
unselect-all取消全选.

例子

这里写图片描述
代码:

#!/usr/bin/env python3
# Created by xiaosanyu at 16/6/13
# section 064
TITLE = "FlowBox"
DESCRIPTION = """
A Gtk.FlowBox positions child widgets in sequence according to its orientation.
"""
import gi

gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk
import math

colors = ("AliceBlue",
          "AntiqueWhite",
          "AntiqueWhite1",
          "AntiqueWhite2",
          "AntiqueWhite3",
          "AntiqueWhite4",
          "aqua",
          "aquamarine",
          "aquamarine1",
          "aquamarine2",
          "aquamarine3",
          "aquamarine4",
          "azure",
          "azure1",
          "azure2",
          "azure3",
          "azure4",
          "beige",
          "bisque",
          "bisque1",
          "bisque2",
          "bisque3",
          "bisque4",
          "black",
          "BlanchedAlmond",
          "blue",
          "blue1",
          "blue2",
          "blue3",
          "blue4",
          "BlueViolet",
          "brown",
          "brown1",
          "brown2",
          "brown3",
          "brown4",
          "burlywood",
          "burlywood1",
          "burlywood2",
          "burlywood3",
          "burlywood4",
          "CadetBlue",
          "CadetBlue1",
          "CadetBlue2",
          "CadetBlue3",
          "CadetBlue4",
          "chartreuse",
          "chartreuse1",
          "chartreuse2",
          "chartreuse3",
          "chartreuse4",
          "chocolate",
          "chocolate1",
          "chocolate2",
          "chocolate3",
          "chocolate4",
          "coral",
          "coral1",
          "coral2",
          "coral3",
          "coral4",
          "CornflowerBlue",
          "cornsilk",
          "cornsilk1",
          "cornsilk2",
          "cornsilk3",
          "cornsilk4",
          "crimson",
          "cyan",
          "cyan1",
          "cyan2",
          "cyan3",
          "cyan4",
          "DarkBlue",
          "DarkCyan",
          "DarkGoldenrod",
          "DarkGoldenrod1",
          "DarkGoldenrod2",
          "DarkGoldenrod3",
          "DarkGoldenrod4",
          "DarkGray",
          "DarkGreen",
          "DarkGrey",
          "DarkKhaki",
          "DarkMagenta",
          "DarkOliveGreen",
          "DarkOliveGreen1",
          "DarkOliveGreen2",
          "DarkOliveGreen3",
          "DarkOliveGreen4",
          "DarkOrange",
          "DarkOrange1",
          "DarkOrange2",
          "DarkOrange3",
          "DarkOrange4",
          "DarkOrchid",
          "DarkOrchid1",
          "DarkOrchid2",
          "DarkOrchid3",
          "DarkOrchid4",
          "DarkRed",
          "DarkSalmon",
          "DarkSeaGreen",
          "DarkSeaGreen1",
          "DarkSeaGreen2",
          "DarkSeaGreen3",
          "DarkSeaGreen4",
          "DarkSlateBlue",
          "DarkSlateGray",
          "DarkSlateGray1",
          "DarkSlateGray2",
          "DarkSlateGray3",
          "DarkSlateGray4",
          "DarkSlateGrey",
          "DarkTurquoise",
          "DarkViolet",
          "DeepPink",
          "DeepPink1",
          "DeepPink2",
          "DeepPink3",
          "DeepPink4",
          "DeepSkyBlue",
          "DeepSkyBlue1",
          "DeepSkyBlue2",
          "DeepSkyBlue3",
          "DeepSkyBlue4",
          "DimGray",
          "DimGrey",
          "DodgerBlue",
          "DodgerBlue1",
          "DodgerBlue2",
          "DodgerBlue3",
          "DodgerBlue4",
          "firebrick",
          "firebrick1",
          "firebrick2",
          "firebrick3",
          "firebrick4",
          "FloralWhite",
          "ForestGreen",
          "fuchsia",
          "gainsboro",
          "GhostWhite",
          "gold",
          "gold1",
          "gold2",
          "gold3",
          "gold4",
          "goldenrod",
          "goldenrod1",
          "goldenrod2",
          "goldenrod3",
          "goldenrod4",
          "gray",
          "gray0",
          "gray1",
          "gray10",
          "gray100",
          "gray11",
          "gray12",
          "gray13",
          "gray14",
          "gray15",
          "gray16",
          "gray17",
          "gray18",
          "gray19",
          "gray2",
          "gray20",
          "gray21",
          "gray22",
          "gray23",
          "gray24",
          "gray25",
          "gray26",
          "gray27",
          "gray28",
          "gray29",
          "gray3",
          "gray30",
          "gray31",
          "gray32",
          "gray33",
          "gray34",
          "gray35",
          "gray36",
          "gray37",
          "gray38",
          "gray39",
          "gray4",
          "gray40",
          "gray41",
          "gray42",
          "gray43",
          "gray44",
          "gray45",
          "gray46",
          "gray47",
          "gray48",
          "gray49",
          "gray5",
          "gray50",
          "gray51",
          "gray52",
          "gray53",
          "gray54",
          "gray55",
          "gray56",
          "gray57",
          "gray58",
          "gray59",
          "gray6",
          "gray60",
          "gray61",
          "gray62",
          "gray63",
          "gray64",
          "gray65",
          "gray66",
          "gray67",
          "gray68",
          "gray69",
          "gray7",
          "gray70",
          "gray71",
          "gray72",
          "gray73",
          "gray74",
          "gray75",
          "gray76",
          "gray77",
          "gray78",
          "gray79",
          "gray8",
          "gray80",
          "gray81",
          "gray82",
          "gray83",
          "gray84",
          "gray85",
          "gray86",
          "gray87",
          "gray88",
          "gray89",
          "gray9",
          "gray90",
          "gray91",
          "gray92",
          "gray93",
          "gray94",
          "gray95",
          "gray96",
          "gray97",
          "gray98",
          "gray99",
          "green",
          "green1",
          "green2",
          "green3",
          "green4",
          "GreenYellow",
          "grey",
          "grey0",
          "grey1",
          "grey10",
          "grey100",
          "grey11",
          "grey12",
          "grey13",
          "grey14",
          "grey15",
          "grey16",
          "grey17",
          "grey18",
          "grey19",
          "grey2",
          "grey20",
          "grey21",
          "grey22",
          "grey23",
          "grey24",
          "grey25",
          "grey26",
          "grey27",
          "grey28",
          "grey29",
          "grey3",
          "grey30",
          "grey31",
          "grey32",
          "grey33",
          "grey34",
          "grey35",
          "grey36",
          "grey37",
          "grey38",
          "grey39",
          "grey4",
          "grey40",
          "grey41",
          "grey42",
          "grey43",
          "grey44",
          "grey45",
          "grey46",
          "grey47",
          "grey48",
          "grey49",
          "grey5",
          "grey50",
          "grey51",
          "grey52",
          "grey53",
          "grey54",
          "grey55",
          "grey56",
          "grey57",
          "grey58",
          "grey59",
          "grey6",
          "grey60",
          "grey61",
          "grey62",
          "grey63",
          "grey64",
          "grey65",
          "grey66",
          "grey67",
          "grey68",
          "grey69",
          "grey7",
          "grey70",
          "grey71",
          "grey72",
          "grey73",
          "grey74",
          "grey75",
          "grey76",
          "grey77",
          "grey78",
          "grey79",
          "grey8",
          "grey80",
          "grey81",
          "grey82",
          "grey83",
          "grey84",
          "grey85",
          "grey86",
          "grey87",
          "grey88",
          "grey89",
          "grey9",
          "grey90",
          "grey91",
          "grey92",
          "grey93",
          "grey94",
          "grey95",
          "grey96",
          "grey97",
          "grey98",
          "grey99",
          "honeydew",
          "honeydew1",
          "honeydew2",
          "honeydew3",
          "honeydew4",
          "HotPink",
          "HotPink1",
          "HotPink2",
          "HotPink3",
          "HotPink4",
          "IndianRed",
          "IndianRed1",
          "IndianRed2",
          "IndianRed3",
          "IndianRed4",
          "indigo",
          "ivory",
          "ivory1",
          "ivory2",
          "ivory3",
          "ivory4",
          "khaki",
          "khaki1",
          "khaki2",
          "khaki3",
          "khaki4",
          "lavender",
          "LavenderBlush",
          "LavenderBlush1",
          "LavenderBlush2",
          "LavenderBlush3",
          "LavenderBlush4",
          "LawnGreen",
          "LemonChiffon",
          "LemonChiffon1",
          "LemonChiffon2",
          "LemonChiffon3",
          "LemonChiffon4",
          "LightBlue",
          "LightBlue1",
          "LightBlue2",
          "LightBlue3",
          "LightBlue4",
          "LightCoral",
          "LightCyan",
          "LightCyan1",
          "LightCyan2",
          "LightCyan3",
          "LightCyan4",
          "LightGoldenrod",
          "LightGoldenrod1",
          "LightGoldenrod2",
          "LightGoldenrod3",
          "LightGoldenrod4",
          "LightGoldenrodYellow",
          "LightGray",
          "LightGreen",
          "LightGrey",
          "LightPink",
          "LightPink1",
          "LightPink2",
          "LightPink3",
          "LightPink4",
          "LightSalmon",
          "LightSalmon1",
          "LightSalmon2",
          "LightSalmon3",
          "LightSalmon4",
          "LightSeaGreen",
          "LightSkyBlue",
          "LightSkyBlue1",
          "LightSkyBlue2",
          "LightSkyBlue3",
          "LightSkyBlue4",
          "LightSlateBlue",
          "LightSlateGray",
          "LightSlateGrey",
          "LightSteelBlue",
          "LightSteelBlue1",
          "LightSteelBlue2",
          "LightSteelBlue3",
          "LightSteelBlue4",
          "LightYellow",
          "LightYellow1",
          "LightYellow2",
          "LightYellow3",
          "LightYellow4",
          "lime",
          "LimeGreen",
          "linen",
          "magenta",
          "magenta1",
          "magenta2",
          "magenta3",
          "magenta4",
          "maroon",
          "maroon1",
          "maroon2",
          "maroon3",
          "maroon4",
          "MediumAquamarine",
          "MediumBlue",
          "MediumOrchid",
          "MediumOrchid1",
          "MediumOrchid2",
          "MediumOrchid3",
          "MediumOrchid4",
          "MediumPurple",
          "MediumPurple1",
          "MediumPurple2",
          "MediumPurple3",
          "MediumPurple4",
          "MediumSeaGreen",
          "MediumSlateBlue",
          "MediumSpringGreen",
          "MediumTurquoise",
          "MediumVioletRed",
          "MidnightBlue",
          "MintCream",
          "MistyRose",
          "MistyRose1",
          "MistyRose2",
          "MistyRose3",
          "MistyRose4",
          "moccasin",
          "NavajoWhite",
          "NavajoWhite1",
          "NavajoWhite2",
          "NavajoWhite3",
          "NavajoWhite4",
          "navy",
          "NavyBlue",
          "OldLace",
          "olive",
          "OliveDrab",
          "OliveDrab1",
          "OliveDrab2",
          "OliveDrab3",
          "OliveDrab4",
          "orange",
          "orange1",
          "orange2",
          "orange3",
          "orange4",
          "OrangeRed",
          "OrangeRed1",
          "OrangeRed2",
          "OrangeRed3",
          "OrangeRed4",
          "orchid",
          "orchid1",
          "orchid2",
          "orchid3",
          "orchid4",
          "PaleGoldenrod",
          "PaleGreen",
          "PaleGreen1",
          "PaleGreen2",
          "PaleGreen3",
          "PaleGreen4",
          "PaleTurquoise",
          "PaleTurquoise1",
          "PaleTurquoise2",
          "PaleTurquoise3",
          "PaleTurquoise4",
          "PaleVioletRed",
          "PaleVioletRed1",
          "PaleVioletRed2",
          "PaleVioletRed3",
          "PaleVioletRed4",
          "PapayaWhip",
          "PeachPuff",
          "PeachPuff1",
          "PeachPuff2",
          "PeachPuff3",
          "PeachPuff4",
          "peru",
          "pink",
          "pink1",
          "pink2",
          "pink3",
          "pink4",
          "plum",
          "plum1",
          "plum2",
          "plum3",
          "plum4",
          "PowderBlue",
          "purple",
          "purple1",
          "purple2",
          "purple3",
          "purple4",
          "red",
          "red1",
          "red2",
          "red3",
          "red4",
          "RosyBrown",
          "RosyBrown1",
          "RosyBrown2",
          "RosyBrown3",
          "RosyBrown4",
          "RoyalBlue",
          "RoyalBlue1",
          "RoyalBlue2",
          "RoyalBlue3",
          "RoyalBlue4",
          "SaddleBrown",
          "salmon",
          "salmon1",
          "salmon2",
          "salmon3",
          "salmon4",
          "SandyBrown",
          "SeaGreen",
          "SeaGreen1",
          "SeaGreen2",
          "SeaGreen3",
          "SeaGreen4",
          "seashell",
          "seashell1",
          "seashell2",
          "seashell3",
          "seashell4",
          "sienna",
          "sienna1",
          "sienna2",
          "sienna3",
          "sienna4",
          "silver",
          "SkyBlue",
          "SkyBlue1",
          "SkyBlue2",
          "SkyBlue3",
          "SkyBlue4",
          "SlateBlue",
          "SlateBlue1",
          "SlateBlue2",
          "SlateBlue3",
          "SlateBlue4",
          "SlateGray",
          "SlateGray1",
          "SlateGray2",
          "SlateGray3",
          "SlateGray4",
          "SlateGrey",
          "snow",
          "snow1",
          "snow2",
          "snow3",
          "snow4",
          "SpringGreen",
          "SpringGreen1",
          "SpringGreen2",
          "SpringGreen3",
          "SpringGreen4",
          "SteelBlue",
          "SteelBlue1",
          "SteelBlue2",
          "SteelBlue3",
          "SteelBlue4",
          "tan",
          "tan1",
          "tan2",
          "tan3",
          "tan4",
          "teal",
          "thistle",
          "thistle1",
          "thistle2",
          "thistle3",
          "thistle4",
          "tomato",
          "tomato1",
          "tomato2",
          "tomato3",
          "tomato4",
          "turquoise",
          "turquoise1",
          "turquoise2",
          "turquoise3",
          "turquoise4",
          "violet",
          "VioletRed",
          "VioletRed1",
          "VioletRed2",
          "VioletRed3",
          "VioletRed4",
          "wheat",
          "wheat1",
          "wheat2",
          "wheat3",
          "wheat4",
          "white",
          "WhiteSmoke",
          "yellow",
          "yellow1",
          "yellow2",
          "yellow3",
          "yellow4",
          "YellowGreen",
          )


class FlowBoxWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="FlowBox Demo")
        self.set_border_width(10)
        self.set_default_size(300, 250)

        header = Gtk.HeaderBar(title="Flow Box")
        header.set_subtitle("Sample FlowBox app")
        header.props.show_close_button = True

        self.set_titlebar(header)

        scrolled = Gtk.ScrolledWindow()
        scrolled.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC)

        flowbox = Gtk.FlowBox()
        flowbox.set_valign(Gtk.Align.START)
        flowbox.set_max_children_per_line(30)
        flowbox.set_selection_mode(Gtk.SelectionMode.NONE)

        self.create_flowbox(flowbox)

        scrolled.add(flowbox)

        self.add(scrolled)
        self.show_all()

    def color_swatch_new(self, str_color):
        color = Gdk.color_parse(str_color)

        rgba = Gdk.RGBA.from_color(color)
        # 可以用ColorButton代替,可直接显示颜色
        # button = Gtk.ColorButton(color=color)
        button = Gtk.Button()

        area = Gtk.DrawingArea()
        area.set_size_request(24, 24)
        # area.override_background_color(0, rgba)
        button.add(area)
        # 连接draw回调
        area.connect("draw", self.draw_callback, rgba)
        return button

    def create_flowbox(self, flowbox):  
        for color in colors:
            button = self.color_swatch_new(color)
            flowbox.add(button)

    # 必须实现draw回调函数才可以显示
    def draw_callback(self, drawingArea, cairo_cxt, rgba):
        # context = drawingArea.get_style_context()
        # width = drawingArea.get_allocated_width()
        # height = drawingArea.get_allocated_height()

        Gdk.cairo_set_source_rgba(cairo_cxt, rgba)
        cairo_cxt.paint()
        # Gtk.render_background(context, cairo_cxt, 0, 0, width, height)

        # 下列几行可以自定义画图,不过要要把Gtk.render_background 这句屏蔽掉

        # cairo_cxt.arc(
        #         width / 2.0, height / 2.0,
        #         min(width, height) / 2.0,
        #         0, 2 * math.pi)
        # # cairo_cxt.rectangle(0, 0, width, height)
        # inner_rgba = Gtk.StyleContext.get_background_color(context, Gtk.StyleContext.get_state(context))
        # Gdk.cairo_set_source_rgba(cairo_cxt, inner_rgba)
        # cairo_cxt.fill()


def main():
    win = FlowBoxWindow()
    win.connect("delete-event", Gtk.main_quit)
    win.show_all()
    Gtk.main()


if __name__ == "__main__":
    main()

代码解析

header = Gtk.HeaderBar(title="Flow Box")
header.set_subtitle("Sample FlowBox app")
header.props.show_close_button = True

self.set_titlebar(header)

创建一个Gtk.HeaderBar,用来代替默认样式的titlebar

scrolled = Gtk.ScrolledWindow()
scrolled.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC)

创建一个Gtk.ScrolledWindow

flowbox = Gtk.FlowBox()
flowbox.set_valign(Gtk.Align.START)
flowbox.set_max_children_per_line(30)
        flowbox.set_selection_mode(Gtk.SelectionMode.NONE)

创建一个Gtk.FlowBox,设置每行孩子最多为30个,设置不可选择

self.create_flowbox(flowbox)

调用create_flowbox方法

def create_flowbox(self, flowbox):
        for color in colors:
            button = self.color_swatch_new(color)
            flowbox.add(button)

使用所给的颜色,调用color_swatch_new方法创建button

 def color_swatch_new(self, str_color):
        color = Gdk.color_parse(str_color)

        rgba = Gdk.RGBA.from_color(color)
        # 可以用ColorButton代替,可直接显示颜色
        # button = Gtk.ColorButton(color=color)
        button = Gtk.Button()

        area = Gtk.DrawingArea()
        area.set_size_request(24, 24)
        # area.override_background_color(0, rgba)
        button.add(area)
        # 连接draw回调
        area.connect("draw", self.draw_callback, rgba)
        return button

将所给的颜色值转换成Gdk.RGBA
创建一个Gtk.Button,在其中添加一个Gtk.DrawingArea,连接Gtk.DrawingArea的“draw”信号

def draw_callback(self, drawingArea, cairo_cxt, rgba):
        Gdk.cairo_set_source_rgba(cairo_cxt, rgba)
        cairo_cxt.paint()

在回调函数中用所给的颜色填充背景

最后将flowbox添加到scrolledview中,scrolledview添加到当前窗口

scrolled.add(flowbox)
self.add(scrolled)

附录

Gtk.SelectionMode

class Gtk.SelectionMode
Bases: GObject.GEnum

NONE = 0

不可选择

SINGLE = 1

0或1个元素可被选择

BROWSE = 2

必须有一个被选择

MULTIPLE = 3

多选。可按Ctrl键或Shift键





代码下载地址:http://download.csdn.net/detail/a87b01c14/9594728

最后

以上就是平淡八宝粥为你收集整理的PyGobject(四十七)布局容器之FlowBoxGtk.FlowBox附录的全部内容,希望文章能够帮你解决PyGobject(四十七)布局容器之FlowBoxGtk.FlowBox附录所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(40)

评论列表共有 0 条评论

立即
投稿
返回
顶部