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

标签 python toggle gtk3 gtktreeview

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

在切换时,我想按某种定义的顺序旋转,例如像这样: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 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.__version__:3.22.0

关于python - 自定义 Gtk.CellRenderer,在 True、None(显示为不一致)和 False 之间切换循环,python GTK 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59524524/

相关文章:

find - 在原子编辑器中切换或关闭查找窗口

javascript - 如何让带有 anchor 标签的url显示隐藏的div

python - 快速将原始文件转换为带水印的 pdf 到 swf

python - CherryPy:需要异常处理帮助

Python:pickle 派生的 str 对象

jquery - 迷你 jQuery 脚本使 div 消失而不是替换其内容

python - 如何使用 Gtk.events_pending?

ubuntu - Matplotlib + Ubuntu + GTK3 未显示绘图

c - 如何设置指向结构的 Gtk gpointer?

python - 日期时间与字符串的比较?