I have a button and I want that the selected row blinks. Someone should click the blink button and then click event to_blink
start a Thread _blink
which change the values of the 3-6 str elements at MyTreeStore
.
import threading
import time
class MyTreeStore(Gtk.TreeStore):
def __init__(self):
# i use the 3 last str's for the background colors
Gtk.TreeStore.__init__(self, str, str, str, str, str, str)
class TestBox(Gtk.VBox):
def __init__(self):
self.treestore = MyTreeStore()
self.treeview = Gtk.TreeView()
self.treeview.set_model(self.treestore)
renderer_col1 = Gtk.CellRendererText()
column_1 = Gtk.TreeViewColumn("Col1", renderer_col1, text=0, cell_background=3)
...
self.blink_button = Gtk.Button('Blink')
self.is_connected_button.connect('clicked', self.to_blink)
def to_blink(self, button):
""" take certain row, start thread which change background-color """
tree_selection = self.treeview.get_selection()
tree_model, treepath = tree_selection.get_selected()
if treepath:
tree_model[treepath][3] = "green"
tree_model[treepath][4] = "green"
tree_model[treepath][5] = "green"
t = threading.Thread(target=self._blink, args=(tree_model[treepath],))
t.daemon = True
t.start()
def _blink(self, path):
for i in range(100):
path[3] = "green"
path[4] = "green"
path[5] = "green"
time.sleep(1)
path[3] = "white"
path[4] = "white"
path[5] = "white"
You should avoid threading as much as possible when working with GTK. In this case this could be solved with GLib.timeout_add
instead.
Replace:
t = threading.Thread(target=self._blink, args=(tree_model[treepath],))
t.daemon = True
t.start()
With:
GLib.timeout_add(1000, self._blink_glib, tree_model[treepath])
def _blink_glib(self, path):
for i in range(3, 6):
if path[i] == "white":
path[i] = "green"
else:
path[i] = "white"
return True
The callback needs to return True
to keep going or False
to stop. So you still have to implement a flag to indicate if the 100 iterations are done.
Just another note: if all columns will blink in the same colour it's not needed to create three extra treestore items, just one would suffice and point all three columns' cell_background
to that item.
column_1 = Gtk.TreeViewColumn("Col1", renderer_col1, text=0, cell_background=3)
column_2 = Gtk.TreeViewColumn("Col2", renderer_col2, text=0, cell_background=3)
column_3 = Gtk.TreeViewColumn("Col3", renderer_col3, text=0, cell_background=3)
def _blink_glib(self, path):
path[3] = "green" if path[3] == "white" else "white"
return True