Search code examples
pythonlinuxmultithreadingpygtk

Showing a dialog in a thread


I have a pygtk add which has a gtk.Button() which when pressed has to do a job of lets say 5 seconds. So, the thought of implementing the time-intensive function in the signal handler as a thread.

First of all, I have added gtk.gdk.threads_init() in the beginning.

My signal handler is def send_sms() and in that I have another function def send_sms_mycantos() which I call as a thread.

threading.Thread(target = self.send_sms_mycantos, args=(phone_no, message_text, username, password)).start()

I also have a function which displays dialogs.

def dialog_notification(self, message, dialog_type):
    dlg = gtk.MessageDialog(self.window, gtk.DIALOG_DESTROY_WITH_PARENT, dialog_type, gtk.BUTTONS_CLOSE, message)
    dlg.run()
    dlg.destroy()

Now, if I call the above function in my thread

self.dialog_notification("Message sent successfully", gtk.MESSAGE_INFO)

I get this error.

SMSSender.py: Fatal IO error 11 (Resource temporarily unavailable) on X server :0.0.

Is this the right way to implement threading. What am I doing wrong. The error is like the thread doesn't know that Xserver is running.


Solution

  • The simplest way would be to run self.dialog_notification() in the main gtk thread. Just add gobject.idle_add(self.dialog_notification, args...) in your thread.