oxidworks oxidworks - 3 months ago 22
Python Question

Create Gtk3 Treeview with CellRendererToggle and Names from List with Python

I try to create a Gtk.TreeView with header names from a list. Because I want to use it to show entries from different databases later.

The problem:
When I click on a cell and try to activate it all cells in the row getting activated. More strange, they look not activated, only if I move the cursor over them (not clicking).

What is the problem with this code?

#!/usr/bin/env python
#-*- coding: utf-8 -*-

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


class CellRendererToggleWindow(Gtk.Window):

def __init__(self):
Gtk.Window.__init__(self, title="CellRendererToggle Example")
self.set_default_size(400, 200)
self.mainbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing = 10)
self.add(self.mainbox)

self.myliststore = Gtk.ListStore(bool,bool,str)
self.treeview = Gtk.TreeView(self.myliststore)

for n, header_text in enumerate(["Aktive", "Warning", "Text"]):
if header_text in ["Aktive", "Warning"]:
cell = Gtk.CellRendererToggle()
cell.connect("toggled", self.on_sync_treeview_button_toggled, n, header_text )
column = Gtk.TreeViewColumn(header_text, cell)
else:
cell = Gtk.CellRendererText()
cell.set_property('editable', True)
column = Gtk.TreeViewColumn(header_text, cell, text=n)
column.set_sort_column_id(n)
self.treeview.append_column(column)
self.myliststore.append([True, True, "Super6!"])
self.myliststore.append([True, True, "Super7!"])
self.myliststore.append([True, True, "Super8!"])
self.mainbox.pack_start(self.treeview, True, True, 0)

def on_sync_treeview_button_toggled(self, widget, path, column, data):
widget.set_active( [True,False][widget.get_active()] )


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

Answer

TreeViewColumn needs an attribute for "active" with the position in the store:

column.add_attribute(cell, "active", n)

And I also had to change the underlying store and not the widget:

self.myliststore[path][column] = not self.myliststore[path][column]

Thanks for this example: https://github.com/Programmica/python-gtk3-tutorial/blob/master/_examples/cellrenderertoggle.py

Now it is working:

#!/usr/bin/env python
#-*- coding: utf-8 -*-

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


class CellRendererToggleWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="CellRendererToggle Example")
        self.set_default_size(400, 200)
        self.mainbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing = 10)
        self.add(self.mainbox)

        self.myliststore = Gtk.ListStore(bool,bool,str)
        self.treeview = Gtk.TreeView(self.myliststore)

        for n, header_text in enumerate(["Aktive", "Warning", "Text"]):
            if header_text in ["Aktive", "Warning"]:
                cell = Gtk.CellRendererToggle()
                cell.connect("toggled", self.on_sync_treeview_button_toggled, n, header_text )
                column = Gtk.TreeViewColumn(header_text, cell)
                column.add_attribute(cell, "active", n)

            else:
                cell = Gtk.CellRendererText()
                cell.set_property('editable', True)
                column = Gtk.TreeViewColumn(header_text, cell, text=n)
            column.set_sort_column_id(n)
            self.treeview.append_column(column)
        self.myliststore.append([True, False, "Super6!"])
        self.myliststore.append([True, True, "Super7!"])
        self.myliststore.append([False, True, "Super8!"])
        self.mainbox.pack_start(self.treeview, True, True, 0)

    def on_sync_treeview_button_toggled(self, widget, path, column, data):
        self.myliststore[path][column] = not self.myliststore[path][column]


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