PyGObject Gtk.TreeView 中的微调器不旋转

Spinner in a PyGObject Gtk.TreeView does not spin

我不清楚如何在以 Gtk.ListStore 为模型的 Gtk.TreeView 中使用 Gtk.SpinnerCellRenderer

在此示例中,显示了微调器,但它不活动。它不旋转。

#!/usr/bin/env python3
import random
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

class TreeView(Gtk.TreeView):
    def __init__(self):
        # model
        self.model = Gtk.ListStore.new([bool, int])
        for i in range(5):
            self.model.append([random.choice([True, True, False]), i])

        # view
        Gtk.TreeView.__init__(self, self.model)

        # column bool (as spinner)
        self.spinner_renderer = Gtk.CellRendererSpinner.new()
        col_bool = Gtk.TreeViewColumn('bool',
                                      self.spinner_renderer,
                                      active=0)
        self.append_column(col_bool)

        # column int
        col_int = Gtk.TreeViewColumn('int',
                                     Gtk.CellRendererText(),
                                     text=1)
        self.append_column(col_int)


class Window(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title='Mein Gtk-Fenster')
        self.set_default_size(100, 120)

        self.view = TreeView()
        self.add(self.view)

        self.connect('destroy', Gtk.main_quit)
        self.show_all()

if __name__ == '__main__':
    win = Window()
    Gtk.main()

来自docs

To start the animation in a cell, set the Gtk.CellRendererSpinner :active property to True and increment the Gtk.CellRendererSpinner :pulse property at regular intervals. The usual way to set the cell renderer properties for each cell is to bind them to columns in your tree model using e.g. Gtk.TreeViewColumn.add_attribute().

#!/usr/bin/env python3
import random
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import GObject

class TreeView(Gtk.TreeView):
    def __init__(self):
        # model
        self.model = Gtk.ListStore.new([bool, int])
        for i in range(5):
            self.model.append([random.choice([True, True, False]), i])

        # view
        Gtk.TreeView.__init__(self, self.model)

        # column bool (as spinner)
        self.spinner_renderer = Gtk.CellRendererSpinner()
        col_bool = Gtk.TreeViewColumn('bool', self.spinner_renderer, active=0)
        self.append_column(col_bool)

        # column int
        col_int = Gtk.TreeViewColumn('int', Gtk.CellRendererText(), text=1)
        self.append_column(col_int)

    def on_spinner_pulse(self):
        for row in self.model:
            if row[0]:
                if row[1] == 150:
                    row[1] = 0
                else:
                    row[1] += 1
        self.spinner_renderer.set_property("pulse", row[1])
        return True

class Window(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title='CellRendererSpinner')
        self.set_default_size(200, 400)

        self.view = TreeView()
        self.add(self.view)

        self.connect('destroy', Gtk.main_quit)
        self.show_all()

if __name__ == '__main__':
    win = Window()
    GObject.timeout_add(100, win.view.on_spinner_pulse)
Gtk.main()

increment the Gtk.CellRendererSpinner :pulse property at regular intervals

  1. 通过在方法中调用self.spinner_renderer.set_property("pulse", newValue)增加cellrendererspinner的"pulse"值

  2. GObject.timeout_add(milliseconds, method)函数重复调用该方法