自定义Gtk.CellRenderer,在True、None(显示为不一致)和False之间循环切换,python Gtk 3

2024-09-29 20:22:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要编写一个定制的Gtk.CellRenderer(我们称之为CellRendererToogleWithNone),它的行为类似于Gtk.CellRendererToggle,但不是只支持TrueFalse,我还希望它支持None,它应该显示为如下不一致状态:inconsistent state pygtk

在toggle上,我想按定义的顺序旋转,例如:True->;错误->;没有 (但这可能是最简单的部分,我仍然认为我提到了这一点)

我也不知道如何使它与TreeStore的工作,因为如果我这样做

self.treestore = Gtk.TreeStore.new(types = (bool,))
iter = self.treestore.append(parent = None, row = (None,))

它将任何None值转换为False,因为bool似乎不允许None

我在网上找不到任何有用的自定义Gtk.CellRenderer示例

我想通过从Gtk.CellRenderer继承而不是从Gtk.CellRendererToggle继承来实现它,因为这也可以作为一个有用的示例来实现更多这样的单元呈现器

我只能猜测我必须为TreeStore定义一些自定义数据类型,类似于bool_or_none而不是bool(也不知道如何做),并在我的自定义CellRendererToogleWithNone中保存我自己的Gtk.ToggleButton

编辑0:

This post关于如何编写自定义Gtk.CellRenderer给出了一些提示,这些提示可能可以重用,但并不能解决我的问题。它没有显示如何使Gtk.TreeStore接受None变量的值,我不理解那里的所有内容。它甚至没有使用Gtk.Button,相反,它似乎在一个小部件中画了一个框,我猜这个小部件可能是父部件。我不想绘制自己的切换,我想重用Gtk.ToggleButton及其inconsistent属性

编辑1:

因为定制单元渲染器似乎并不容易,所以我认为在python中看到一个最小的工作示例会特别有用。我还应该提到,我希望尽可能简洁地显示数据,这排除了建议的解决方法,例如一个值有两个切换


Tags: gtselfnonefalsetrue示例gtk定义
1条回答
网友
1楼 · 发布于 2024-09-29 20:22:47

Question: Custom Gtk.CellRendererToggle with a Toggle cycling between True, None (displayed as inconsistent) and False.

你被误导了,设置inconsistant标志不是Gtk.CellRendererToggle对象的一部分。已经准备好了。您必须实现Gtk.TreeCellDataFunc

I also want it to support None

I don't get it to work with a None value,
therefore i use type int with values: [False, True, 2]

注意:

  • def on_toggle(...)中,model值更改为0 == False1 == True。如果希望它保持为boolean类型,请相应地实现
  • 您应该知道,值2的计算结果也是True

App


参考文献:

  • Gtk.TreeViewColumn.set_cell_data_func

    This function is used instead of the standard attributes mapping for setting the column value, and should set the value of self’s cell renderer as appropriate.

  • Gtk.TreeCellDataFunc

    A function to set the properties of a cell instead of just using the straight mapping between the cell and the model. This is useful for customizing the cell renderer


Implementation: Core point: .set_cell_data_func(cell_renderer, .set_status)

class TreeViewColumnTriState(Gtk.TreeViewColumn):

    def __init__(self, title='', model=None, **attributes):
        cell_renderer = Gtk.CellRendererToggle()
        cell_renderer.connect("toggled", self.on_toggle, model)

        self.inconsistent = attributes.pop('inconsistent', None)
        super().__init__(title, cell_renderer, active=0, **attributes)
        self.set_cell_data_func(cell_renderer, self.set_status)

    def set_status(self, column, cell, model, _iter, *ignore):    
        if model.get_value(_iter, 0) == self.inconsistent:
            cell.set_property('inconsistent', True)
        else:
            cell.set_property('inconsistent', False)

    def on_toggle(self, cell, path, model, *ignore):
        if path is not None:
            model = model[model.get_iter(path)]    
            model[0] += 1
            if model[0] == 3:
                model[0] = 0

Usage:

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


class App(Gtk.ApplicationWindow):
    def __init__(self):
        super().__init__()
        self.connect("destroy", Gtk.main_quit)

        model = Gtk.ListStore(int)

        #  Some initial data
        for n in [False, True, 2]:
            model.append([n])

        col = TreeViewColumnTriState("Foo", model, inconsistent=2)

        tv = Gtk.TreeView(model)
        tv.append_column(col)
        self.add(tv)

        self.show_all()


if __name__ == "__main__":
    main = App()
    Gtk.main()

使用Python:3.5-gi进行测试。\u版本:3.22.0

相关问题 更多 >

    热门问题