Skip to content Skip to sidebar Skip to footer

Gtk +3 Textview Application Crashes

I have an application using a GtkTextView and GtkTextBuffer. Lines are added to the buffer with the following python code which runs in a separate thread from the main process:

Solution 1:

You cannot access the GtkTextBuffer — or any part of GTK+, for that matter — from a separate thread. You must access it from the GUI thread. You will need to use GLib.idle_add() to queue the buffer update on the GUI thread.

Solution 2:

I was confused by the doc I found for GLib because GLib.idle_add does not accept an timeout. http://lazka.github.io/pgi-docs/index.html#GLib-2.0/functions.html#GLib.idle_add

But without timeout it is working for me using Python 2.7.12 and Gtk 3.20

Create a function for text insert:

defset_text(self, text):
    textbuffer = self.textview.get_buffer()
    textbuffer.set_text(text, len(text))

   defappend_text(self, text:
    oldtext = self.get_text()
    newtext = "{}\n{}".format(oldtext, text)
    self.set_text(newtext)

Then you can set new text with:

GLib.idle_add(self.textbox.append_text, "I am the new text line)  

Additional Info:

While the script is running I also check sensitivity of button with:

defon_search_button_clicked(self, widget)
    widget.set_sensitive(False)
    thread = Thread(target=self.run_search_script)
    thread.start()
    GObject.timeout_add(200, self.sensi, thread, widget)

defsensi(self, thread, widget):
    returnTrueif thread.is_alive() else widget.set_sensitive(True)

Post a Comment for "Gtk +3 Textview Application Crashes"