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)