我需要编写一个自定义的Gtk.CellRenderer
(我们称之为CellRendererToogleWithNone
),其行为类似于Gtk.CellRendererToggle
,但不是仅支持 True
和 False
我还希望它支持 None
,它应该显示为不一致的状态,如下所示:
在切换时,我想按某种定义的顺序旋转,例如像这样:True -> False -> None (但这可能是最简单的部分,我仍然认为我提到了这一点)
我也不知道如何让它与 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
(不知道如何做到这一点)并将我自己的 Gtk.ToggleButton
放在我的自定义 CellRendererToogleWithNone
中。
编辑 0:
This post关于如何编写自定义 Gtk.CellRenderer 给出了一些提示,这些提示可能可以重用,但不能解决我的问题。它没有显示如何使 Gtk.TreeStore
接受 bool
变量的 None
值,我不明白那里的一切。它甚至不使用 Gtk.Button,而是似乎在一个小部件内绘制了一个框,我猜测该小部件可能是父级。我不想绘制自己的 Toggle,我想重用 Gtk.ToggleButton
及其 inconfirm
属性
编辑 1:
由于自定义单元格渲染器似乎并不容易,我认为在 python 中查看一个最小的工作示例会特别有用。我还应该提到,我希望尽可能紧凑地显示数据,这排除了建议的解决方法,例如一个值有两个切换。
最佳答案
Question: Custom
Gtk.CellRendererToggle
with a Toggle cycling between True, None (displayed as inconsistent) and False.
你被误导了,这不是 Gtk.CellRendererToggle
的一部分对象设置 inconsistant
旗帜。它已经在那里实现了。您必须实现 Gtk.TreeCellDataFunc
反而。
I also want it to support
None
I don't get it to work with a
None
value,
therefore i use typeint
with values:[False, True, 2]
注意:
block 引用> <小时/>
- 在
def on_toggle(...)
model
值更改为0 == False
和1 == True
。如果您希望它保持boolean
类型,相应实现。- 您应该知道,值
2
,计算结果为True
还。引用:
<小时/>
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.
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.__version__:3.22.0
关于python - 自定义 Gtk.CellRenderer,在 True、None(显示为不一致)和 False 之间切换循环,python GTK 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59524524/