1

I have a function in my main application that looks like this:

def foo(stuff):

   a_line_that_takes_a_while(stuff)

   return result

I'm trying to add a dialog to show before a_line_that_takes_a_while and destroy it right after that line was executed.

I've tried:

def foo(stuff):
   dialog = Gtk.MessageDialog(...)
   dialog.show_all()

   a_line_that_takes_a_while(stuff)

   dialog.destroy()

   return result

But surprisingly, the dialog shows up just when a_line_that_takes_a_while was already executed. Of course I can't use dialog.run() because that'd block my application's main loop.

Any ideas?

joaquinlpereyra
  • 956
  • 7
  • 17
  • I recently replied to a C version of this same question here: http://stackoverflow.com/questions/36971139/gui-becomes-unresponsive-after-clicking-the-button-using-gtk-in-c – ebassi May 05 '16 at 16:14

1 Answers1

3

All that your calls to GTK are really doing is queuing up actions that happen during the main loop. A dialog is somewhat of a special case where when you call dialog.run() to blocks permitting certain updates. Your foo function instructs GTK to create a dialog then destroy it, before it even got started trying to do the work.

Threads should do the job. The biggest gotcha here is that GTK is NOT thread safe. Therefore be careful if you decide to use native python threading. Also, if you are doing disk operations consider GFile's asynchronous callbacks. They might save you a little bit of wheel re-inventing.

Blake
  • 368
  • 1
  • 11