1

I tried to call a message dialog inside a thread (use python3 and gtk3), in the next example when I pressed the button the label start to change the text every 1 seconds, this part work fine, the problem appear when I uncomment the line self.Mensage_Falla("final"), the application close for itself, here is the code to see if somebody can show me the light:

import threading
import time
import serial
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk, Pango, GLib, GdkPixbuf, GObject

class GridWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="TEST")
        self.set_border_width(15)
        self.grid = Gtk.Grid()
        self.grid.set_column_spacing(25)
        self.grid.set_row_spacing(25)
        self.add(self.grid)

        self.label1 = Gtk.Label("NUMERO DE SERIE")
        self.txtSerialNumber = Gtk.Entry()

        btnStartTest=Gtk.Button("Iniciar Prueba")
        btnStartTest.connect("clicked",self.StartTest_Tread)

        self.grid.add(self.label1)
        self.grid.attach(btnStartTest, 0, 1, 1, 1)

    def StartTest_Tread(self,widget):
        self.thread = threading.Thread(target=self.StartTest)
        self.thread.daemon = True
        self.thread.start()

    def StartTest(self):
        self.label1.set_text("xxxx")
        time.sleep(1)
        self.label1.set_text("yyyy")
        time.sleep(1)
        self.label1.set_text("zzzz")
        time.sleep(1)
        self.label1.set_text("aaaa")
        self.Mensage_Falla("final")


    def Mensage_Falla(self, UUT_Failure):
        dialog = Gtk.MessageDialog(self, 0, Gtk.MessageType.INFO,
            Gtk.ButtonsType.OK, "UUT")
        dialog.format_secondary_text(
            "LA UUT A FALLADO LA PRUEBA " + UUT_Failure)
        dialog.run()
        dialog.destroy() 
        print("fALLA")



try:
    GObject.threads_init()
    win = GridWindow()
    win.set_position(Gtk.WindowPosition.CENTER)
    win.set_default_size(100,100)
    win.set_type_hint(Gdk.WindowTypeHint.MENU)
    win.connect("delete-event", Gtk.main_quit)
    win.show_all()
    Gtk.main()
except ValueError as Argument:
    print ("The argument does not contain numbers\n", Argument)
    print("error " + str(Exception.args))
    time.sleep(30)
jcoppens
  • 5,306
  • 6
  • 27
  • 47
user39269
  • 44
  • 4
  • Possible duplicate of [Python GTK+ 3 Safe Threading](https://stackoverflow.com/questions/21150914/python-gtk-3-safe-threading) – Aran-Fey Jun 28 '17 at 22:58
  • Don't do Gtk stuff in a thread. Gtk is not thread safe. Use `idle_add` or `timeout_add` as seen in the linked question above. – Aran-Fey Jun 28 '17 at 22:59
  • Call the dialog message inside GLib.idle_add fix the problem, so this line look is this way: GLib.idle_add(self.Mensage_Falla,"final"). Now I'll try to add a input message box and see the results. – user39269 Jun 29 '17 at 14:23

0 Answers0