PyGobject(五十七)布局容器之TreeView(下)——CellRenderer

Gtk.CellRenderer

Gtk.CellRenderer TreeView中单元格渲染器,用来显示不同类型的数据

继承关系

这里写图片描述

Methods

方法修饰词方法名及参数
activate (event, widget, path, background_area, cell_area, flags)
get_aligned_area (widget, flags, cell_area)
get_alignment ()
get_fixed_size ()
get_padding ()
get_preferred_height (widget)
get_preferred_height_for_width (widget, width)
get_preferred_size (widget)
get_preferred_width (widget)
get_preferred_width_for_height (widget, height)
get_request_mode ()
get_sensitive ()
get_size (widget, cell_area)
get_state (widget, cell_state)
get_visible ()
is_activatable ()
render (cr, widget, background_area, cell_area, flags)
set_alignment (xalign, yalign)
set_fixed_size (width, height)
set_padding (xpad, ypad)
set_sensitive (sensitive)
set_visible (visible)
start_editing (event, widget, path, background_area, cell_area, flags)
stop_editing (canceled)

Virtual Methods

do_activate (event, widget, path, background_area, cell_area, flags)
do_editing_canceled ()
do_editing_started (editable, path)
do_get_aligned_area (widget, flags, cell_area)
do_get_preferred_height (widget)
do_get_preferred_height_for_width (widget, width)
do_get_preferred_width (widget)
do_get_preferred_width_for_height (widget, height)
do_get_request_mode ()
do_get_size (widget, cell_area)
do_render (cr, widget, background_area, cell_area, flags)
do_start_editing (event, widget, path, background_area, cell_area, flags)

Properties

NameTypeFlagsShort Description
cell-backgroundstrwCell background color as a string
cell-background-gdkGdk.Colord/r/wCell background color as a Gdk.Color deprecated
cell-background-rgbaGdk.RGBAr/wCell background color as a Gdk.RGBA
cell-background-setboolr/w/enWhether the cell background color is set
editingboolrWhether the cell renderer is currently in editing mode
heightintr/w/enThe fixed height
is-expandedboolr/w/enRow is an expander row, and is expanded
is-expanderboolr/w/enRow has children
modeGtk.CellRendererModer/w/enEditable mode of the CellRenderer
sensitiveboolr/w/enxlib.Display the cell sensitive
visibleboolr/w/enxlib.Display the cell
widthintr/w/enThe fixed width
xalignfloatr/w/enThe x-align
xpadintr/w/enThe xpad
yalignfloatr/w/enThe y-align
ypadintr/w/enThe ypad

Signals

NameShort Description
editing-canceledThis signal gets emitted when the user cancels the process of editing a cell.
editing-startedThis signal gets emitted when a cell starts to be edited.

Gtk.CellRendererText

继承关系

这里写图片描述

Methods

方法修饰词方法名及参数
staticnew ()
set_fixed_height_from_font (number_of_rows)

Virtual Methods

do_edited (path, new_text)

Properties

NameTypeFlagsShort Description
align-setboolr/wWhether this tag affects the alignment mode
alignmentPango.Alignmentr/w/enHow to align the lines
attributesPango.AttrListr/wA list of style attributes to apply to the text of the renderer
backgroundstrwBackground color as a string
background-gdkGdk.Colord/r/wBackground color as a Gdk.Color deprecated
background-rgbaGdk.RGBAr/wBackground color as a Gdk.RGBA
background-setboolr/wWhether this tag affects the background color
editableboolr/wWhether the text can be modified by the user
editable-setboolr/wWhether this tag affects text editability
ellipsizePango.EllipsizeModer/w/enThe preferred place to ellipsize the string, if the cell renderer does not have enough room to display the entire string
ellipsize-setboolr/wWhether this tag affects the ellipsize mode
familystrr/wName of the font family, e.g. Sans, Helvetica, Times, Monospace
family-setboolr/wWhether this tag affects the font family
fontstrr/wFont description as a string, e.g. “Sans Italic 12”
font-descPango.FontDescriptionr/wFont description as a Pango.FontDescription struct
foregroundstrwForeground color as a string
foreground-gdkGdk.Colord/r/wForeground color as a Gdk.Color deprecated
foreground-rgbaGdk.RGBAr/wForeground color as a Gdk.RGBA
foreground-setboolr/wWhether this tag affects the foreground color
languagestrr/wThe language this text is in, as an ISO code. Pango can use this as a hint when rendering the text. If you don’t understand this parameter, you probably don’t need it
language-setboolr/wWhether this tag affects the language the text is rendered as
markupstrwMarked up text to render
max-width-charsintr/w/enThe maximum width of the cell, in characters
placeholder-textstrr/wText rendered when an editable cell is empty
riseintr/wOffset of text above the baseline (below the baseline if rise is negative)
rise-setboolr/wWhether this tag affects the rise
scalefloatr/wFont scaling factor
scale-setboolr/wWhether this tag scales the font size by a factor
single-paragraph-modeboolr/w/enWhether to keep all text in a single paragraph
sizeintr/wFont size
size-pointsfloatr/wFont size in points
size-setboolr/wWhether this tag affects the font size
stretchPango.Stretchr/wFont stretch
stretch-setboolr/wWhether this tag affects the font stretch
strikethroughboolr/wWhether to strike through the text
strikethrough-setboolr/wWhether this tag affects strikethrough
stylePango.Styler/wFont style
style-setboolr/wWhether this tag affects the font style
textstrr/wText to render
underlinePango.Underliner/wStyle of underline for this text
underline-setboolr/wWhether this tag affects underlining
variantPango.Variantr/wFont variant
variant-setboolr/wWhether this tag affects the font variant
weightintr/wFont weight
weight-setboolr/wWhether this tag affects the font weight
width-charsintr/w/enThe desired width of the label, in characters
wrap-modePango.WrapModer/w/enHow to break the string into multiple lines, if the cell renderer does not have enough room to display the entire string
wrap-widthintr/w/enThe width at which the text is wrapped

Signals

NameShort Description
editedThis signal is emitted after renderer has been edited.

例子

这里写图片描述
代码:

#!/usr/bin/env python3
# Created by xiaosanyu at 16/6/15
# section 081
TITLE = "CellRendererText"
DESCRIPTION = """
A Gtk.CellRendererText renders a given text in its cell,
using the font, color and style information provided by its properties.
The text will be ellipsized if it is too long and the
Gtk.CellRendererText :ellipsize property allows it.
"""
import gi

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


class CellRendererTextWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="CellRendererText Example")

        self.set_default_size(200, 200)

        self.liststore = Gtk.ListStore(str, str)
        self.liststore.append(["Fedora", "http://fedoraproject.org/"])
        self.liststore.append(["Slackware", "http://www.slackware.com/"])
        self.liststore.append(["Sidux", "http://sidux.com/"])

        treeview = Gtk.TreeView(model=self.liststore)

        renderer_text = Gtk.CellRendererText()
        column_text = Gtk.TreeViewColumn("Text", renderer_text, text=0)
        treeview.append_column(column_text)

        renderer_editabletext = Gtk.CellRendererText()
        renderer_editabletext.set_property("editable", True)

        column_editabletext = Gtk.TreeViewColumn("Editable Text",
                                                 renderer_editabletext, text=1)
        treeview.append_column(column_editabletext)

        renderer_editabletext.connect("edited", self.text_edited)

        self.add(treeview)

    def text_edited(self, widget, path, text):
        self.liststore[path][1] = text


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


if __name__ == "__main__":
    main()

Gtk.CellRendererCombo

继承关系

这里写图片描述

Methods

方法修饰词方法名及参数
staticnew ()

Virtual Methods

Properties

NameTypeFlagsShort Description
has-entryboolr/w/enIf False, don’t allow to enter strings other than the chosen ones
modelGtk.TreeModelr/wThe model containing the possible values for the combo box
text-columnintr/w/enA column in the data source model to get the strings from

Signals

NameShort Description
changedThis signal is emitted each time after the user selected an item in the combo box, either by using the mouse or the arrow keys.

例子

这里写图片描述
代码:

#!/usr/bin/env python3
# Created by xiaosanyu at 16/6/15
# section 082
TITLE = "CellRendererCombo"
DESCRIPTION = """
Gtk.CellRendererCombo renders text in a cell like Gtk.CellRendererText from which it is derived.
But while Gtk.CellRendererText offers a simple entry to edit the text,
Gtk.CellRendererCombo offers a Gtk.ComboBox widget to edit the text.
The values to display in the combo box are taken from the tree model specified
in the Gtk.CellRendererCombo :model property
"""
import gi

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


class CellRendererComboWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="CellRendererCombo Example")

        self.set_default_size(200, 200)

        liststore_manufacturers = Gtk.ListStore(str)
        manufacturers = ["Sony", "LG",
                         "Panasonic", "Toshiba", "Nokia", "Samsung"]
        for item in manufacturers:
            liststore_manufacturers.append([item])

        self.liststore_hardware = Gtk.ListStore(str, str)
        self.liststore_hardware.append(["Television", "Samsung"])
        self.liststore_hardware.append(["Mobile Phone", "LG"])
        self.liststore_hardware.append(["DVD Player", "Sony"])

        treeview = Gtk.TreeView(model=self.liststore_hardware)

        renderer_text = Gtk.CellRendererText()
        column_text = Gtk.TreeViewColumn("Text", renderer_text, text=0)
        treeview.append_column(column_text)

        renderer_combo = Gtk.CellRendererCombo()
        renderer_combo.set_property("editable", True)
        renderer_combo.set_property("model", liststore_manufacturers)
        renderer_combo.set_property("text-column", 0)
        renderer_combo.set_property("has-entry", False)
        renderer_combo.connect("edited", self.on_combo_changed)

        column_combo = Gtk.TreeViewColumn("Combo", renderer_combo, text=1)
        treeview.append_column(column_combo)

        self.add(treeview)

    def on_combo_changed(self, widget, path, text):
        self.liststore_hardware[path][1] = text


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


if __name__ == "__main__":
    main()

Gtk.CellRendererSpin

继承关系

这里写图片描述

Methods

方法修饰词方法名及参数
staticnew ()

Virtual Methods

Properties

NameTypeFlagsShort Description
adjustmentGtk.Adjustmentr/wThe adjustment that holds the value of the spin button
climb-ratefloatr/wThe acceleration rate when you hold down a button
digitsintr/w/enThe number of decimal places to display

Signals

NameShort Description

例子

这里写图片描述
代码:

#!/usr/bin/env python3
# Created by xiaosanyu at 16/6/15
# section 083
TITLE = "CellRendererSpin"
DESCRIPTION = """
Gtk.CellRendererSpin renders text in a cell like Gtk.CellRendererText from which it is derived.
But while Gtk.CellRendererText offers a simple entry to edit the text,
Gtk.CellRendererSpin offers a Gtk.SpinButton widget. Of course,
that means that the text has to be parseable as a floating point number.
"""
import gi

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


class CellRendererSpinWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="CellRendererSpin Example")

        self.set_default_size(200, 200)

        self.liststore = Gtk.ListStore(str, int)
        self.liststore.append(["Oranges", 5])
        self.liststore.append(["Apples", 4])
        self.liststore.append(["Bananas", 2])

        treeview = Gtk.TreeView(model=self.liststore)

        renderer_text = Gtk.CellRendererText()
        column_text = Gtk.TreeViewColumn("Fruit", renderer_text, text=0)
        treeview.append_column(column_text)

        renderer_spin = Gtk.CellRendererSpin()
        renderer_spin.connect("edited", self.on_amount_edited)
        renderer_spin.set_property("editable", True)

        adjustment = Gtk.Adjustment(0, 0, 100, 1, 10, 0)
        renderer_spin.set_property("adjustment", adjustment)

        column_spin = Gtk.TreeViewColumn("Amount", renderer_spin, text=1)
        treeview.append_column(column_spin)

        self.add(treeview)

    def on_amount_edited(self, widget, path, value):
        self.liststore[path][1] = int(value)


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


if __name__ == "__main__":
    main()

Gtk.CellRendererPixbuf

继承关系

这里写图片描述

Methods

方法修饰词方法名及参数
staticnew ()

Virtual Methods

Properties

NameTypeFlagsShort Description
follow-stateboold/r/wWhether the rendered pixbuf should be colorized according to the state deprecated
giconGio.Iconr/wThe Gio.Icon being displayed
icon-namestrr/wThe name of the icon from the icon theme
pixbufGdkPixbuf.Pixbufr/wThe pixbuf to render
pixbuf-expander-closedGdkPixbuf.Pixbufr/wPixbuf for closed expander
pixbuf-expander-openGdkPixbuf.Pixbufr/wPixbuf for open expander
stock-detailstrr/wRender detail to pass to the theme engine
stock-idstrd/r/wThe stock ID of the stock icon to render deprecated
stock-sizeintr/wThe Gtk.IconSize value that specifies the size of the rendered icon
surfacecairo.Surfacer/wThe surface to render

Signals

NameShort Description

例子

这里写图片描述
代码:

#!/usr/bin/env python3
# Created by xiaosanyu at 16/6/15
# section 084
TITLE = "CellRendererPixbuf"
DESCRIPTION = """
A Gtk.CellRendererPixbuf can be used to render an image in a cell.
"""
import gi

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


class CellRendererPixbufWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="CellRendererPixbuf Example")

        self.set_default_size(200, 200)

        self.liststore = Gtk.ListStore(str, str)
        self.liststore.append(["New", "document-new"])
        self.liststore.append(["Open", "document-open"])
        self.liststore.append(["Save", "document-save"])

        treeview = Gtk.TreeView(model=self.liststore)

        renderer_text = Gtk.CellRendererText()
        column_text = Gtk.TreeViewColumn("Text", renderer_text, text=0)
        treeview.append_column(column_text)

        renderer_pixbuf = Gtk.CellRendererPixbuf()

        column_pixbuf = Gtk.TreeViewColumn("Image", renderer_pixbuf, icon_name=1)
        treeview.append_column(column_pixbuf)

        self.add(treeview)


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


if __name__ == "__main__":
    main()

Gtk.CellRendererSpinner

继承关系

这里写图片描述

Methods

方法修饰词方法名及参数
staticnew ()

Virtual Methods

Properties

NameTypeFlagsShort Description
activeboolr/w/enWhether the spinner is active (ie. shown) in the cell
pulsevintr/w/enPulse of the spinner
sizeGtk.IconSizer/w/enThe Gtk.IconSize value that specifies the size of the rendered spinner

Signals

NameShort Description

例子

这里写图片描述
代码:

#!/usr/bin/env python3
# Created by xiaosanyu at 16/6/15
# section 086
TITLE = "CellRendererProgress"
DESCRIPTION = """
Gtk.CellRendererSpinner renders a spinning animation in a cell, very similar to Gtk.Spinner.
It can often be used as an alternative to a Gtk.CellRendererProgress for displaying indefinite activity,
instead of actual progress
"""
import gi

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


class CellRendererSpinnerWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="CellRendererSpinner Example")

        self.set_default_size(200, 200)

        self.liststore = Gtk.ListStore(str, int, bool)
        self.current_iter = self.liststore.append(["Sabayon", 0, True])
        self.liststore.append(["Zenwalk", 0, True])
        self.liststore.append(["SimplyMepis", 0, True])

        treeview = Gtk.TreeView(model=self.liststore)

        renderer_text = Gtk.CellRendererText()
        column_text = Gtk.TreeViewColumn("Text", renderer_text, text=0)
        treeview.append_column(column_text)

        renderer_spinner = Gtk.CellRendererSpinner()
        column_spinner = Gtk.TreeViewColumn("Spinning", renderer_spinner, pulse=1,
                                            active=2)
        treeview.append_column(column_spinner)

        renderer_toggle = Gtk.CellRendererToggle()
        renderer_toggle.connect("toggled", self.on_inverted_toggled)
        column_toggle = Gtk.TreeViewColumn("Active", renderer_toggle,
                                           active=2)
        treeview.append_column(column_toggle)

        self.add(treeview)

        self.timeout_id = GObject.timeout_add(100, self.on_timeout, None)

    def on_inverted_toggled(self, widget, path):
        self.liststore[path][2] = not self.liststore[path][2]

    def on_timeout(self, user_data):
        if self.liststore is None:
            return False
        for row in self.liststore:
            pulse, active = row[1], row[2]
            if pulse == 999999999:
                pulse = 0
            else:
                pulse += 1

            # self.liststore.set(row.iter, (1, 2), (pulse, active))
            row[1], row[2] = pulse, active

        return True


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


if __name__ == "__main__":
    main()

Gtk.CellRendererToggle

继承关系

这里写图片描述

Methods

方法修饰词方法名及参数
staticnew ()
get_activatable ()
get_active ()
get_radio ()
set_activatable (setting)
set_active (setting)
set_radio (radio)

Virtual Methods

do_toggled (path)

Properties

NameTypeFlagsShort Description
activatableboolr/w/enThe toggle button can be activated
activeboolr/w/enThe toggle state of the button
inconsistentboolr/w/enThe inconsistent state of the button
indicator-sizeintd/r/w/enSize of check or radio indicator deprecated
radioboolr/w/enDraw the toggle button as a radio button

Signals

NameShort Description
toggledThe ::toggled signal is emitted when the cell is toggled.

例子

这里写图片描述
代码:

#!/usr/bin/env python3
# Created by xiaosanyu at 16/6/15
# section 087
TITLE = "CellRendererToggle"
DESCRIPTION = """
Gtk.CellRendererToggle renders a toggle button in a cell.
The button is drawn as a radio or a checkbutton, depending on the
Gtk.CellRendererToggle :radio property. When activated,
it emits the Gtk.CellRendererToggle ::toggled signal.
"""
import gi

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


class CellRendererToggleWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="CellRendererToggle Example")

        self.set_default_size(200, 200)

        self.liststore = Gtk.ListStore(str, bool, bool)
        self.liststore.append(["Debian", False, True])
        self.liststore.append(["OpenSuse", True, False])
        self.liststore.append(["Fedora", False, False])

        treeview = Gtk.TreeView(model=self.liststore)

        renderer_text = Gtk.CellRendererText()
        column_text = Gtk.TreeViewColumn("Text", renderer_text, text=0)
        treeview.append_column(column_text)

        renderer_toggle = Gtk.CellRendererToggle()
        renderer_toggle.connect("toggled", self.on_cell_toggled)

        column_toggle = Gtk.TreeViewColumn("Toggle", renderer_toggle, active=1)
        treeview.append_column(column_toggle)

        renderer_radio = Gtk.CellRendererToggle()
        renderer_radio.set_radio(True)
        renderer_radio.connect("toggled", self.on_cell_radio_toggled)

        column_radio = Gtk.TreeViewColumn("Radio", renderer_radio, active=2)
        treeview.append_column(column_radio)

        self.add(treeview)

    def on_cell_toggled(self, widget, path):
        self.liststore[path][1] = not self.liststore[path][1]

    def on_cell_radio_toggled(self, widget, path):
        selected_path = Gtk.TreePath(path)
        for row in self.liststore:
            row[2] = (row.path == selected_path)


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


if __name__ == "__main__":
    main()




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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sanxiaochengyu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值