1

After launching a process using subprocess.Popen() as shown, I would like to create a busy-window (wx.Dialog) with an "Abort"-button. This works as intended; however, in the case where the process is given the time needed to finish, the busy-window should be destroyed. In the code shown below, this does not happen?

import subprocess
import wx

ProcessToCall = [Some, process, with, some, arguments]

Process = subprocess.Popen(ProcessToCall)
BusyDialog = wx.Dialog(...)

if BusyDialog.ShowModal() == wx.ID_CANCEL:
    Process.kill()

Process.wait()
BusyDialog.Destroy()

I've been experimenting with EndModal and various other methods of wx.Dialog and consulted various tutorials online, but nothing seems to achieve the desired effect, and I'm all out of ideas.

I'm using Python 2.7 and wxPython 2.8 on Ubuntu 13.10.

Hmmmmm
  • 37
  • 6

1 Answers1

1

I think the problem is actually ShowModal, which won't exit until the user clicks something in the window to make it exit. From the docs:

Shows a modal dialog. Program flow does not return until the dialog has been dismissed with wxDialog::EndModal.

I think ShowModal is fine and appropriate, but you need to pass the process to the dialog, and the dialog probably needs some kind of periodic checking for the process to complete (perhaps EVT_TIMER) so that it can close itself and return when the process completes. It will still return the abort code if a user cancels, so you still want to catch that and kill the process if you see that.

Corley Brigman
  • 11,633
  • 5
  • 33
  • 40
  • ave no You are correct - thanks! I still not sure how to implement a working solution though, but thanks for the debugging :). I'll experiment with passing the process to the window as well. – Hmmmmm Mar 13 '14 at 15:57
  • You'll have to change your flow from `Process.wait()`, which does the waiting internally and doesn't return until the process is complete, to a series of `if not Process.poll(): kill process`. Inside the dialog, the best way to do that is to set up a timer, and catch the timer events. Each time, just check the process once. If it's no longer running, call `EndModal()` with a success return code. Otherwise, just return. Something like that. – Corley Brigman Mar 13 '14 at 16:17
  • I would probably call the subprocess from within the dialog subclass. That way the subprocess ends, you can just close the dialog from within. – Mike Driscoll Mar 13 '14 at 16:42
  • Thanks for your insights - I got it working using wx.Timer as suggested. Calling the subprocess from inside the dialog would likely work out fine as well. Thanks for your insight. – Hmmmmm Mar 13 '14 at 18:18