0
votes

This is a follow-up to my previous question, wxPython popup from calling imported function.

I was able to figure out a way to create a wxPython dialog window to determine if a thread my GUI called should continue its execution. What I did was simply create the dialog window in the thread itself. However, once I made the thread exit by clicking "no" in this popup, the close button in my GUI became unresponsive, and I couldn't close the GUI itself. Once again, your help is much appreciated!

GUI code:

import sys
import os
import re
import subprocess
import threading
import wx
import errno, os, stat, shutil
import extern_func

#this object redirects the external function output to the text box
class RedirectText(object):
    def __init__(self,aWxTextCtrl):
        self.out=aWxTextCtrl

    def write(self,string):
        self.out.WriteText(string)

#GUI code here
class progFrame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, title="functionGUI", size=(800, 600), style=wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER)
        panel = wx.Panel(self)

        #more things....

        self.closeButton = wx.Button(panel, wx.ID_OK, "Run", pos=(250, 300))
        self.runButton = wx.Button(panel, wx.ID_OK, "Run", pos=(200, 300))

        self.out=wx.TextCtrl(panel, style=wx.TE_MULTILINE|wx.VSCROLL|wx.TE_READONLY, pos = (300, 50), size=(500, 200))

        #Run button event
        self.Bind(wx.EVT_BUTTON, self.OnRun, self.runButton)

        #close button event
        self.Bind(wx.EVT_BUTTON, self.OnClose, self.closeButton)

        #command prompt output to frame
        redir=RedirectText(self.out)
        sys.stdout=redir
        self.Show()

    def OnRun(self, event):
        t=threading.Thread(target=self.__run)
        t.start()

    def OnClose(self, event):
        self.Destroy()

    #external function call
    def __run(self):
        externFunc()

if __name__ == '__main__':
app = wx.App(False)
progFrame(None)
app.MainLoop()

External function code:

import sys

def externFunc():
    print "Starting execution..."
    #a bunch of code...

    #this is the code for the Yes/No prompt and what introduced the buggy behavior
    if(os.path.isdir(mirror_source_path) or os.path.isdir(mirror_dest_path)):
        app = wx.App(False)
        dlg = wx.MessageDialog(None, "Something bad happened. Continue?","Warning",wx.YES_NO | wx.ICON_QUESTION)
        retCode = dlg.ShowModal()
        if (retCode == wx.ID_YES):
            print "Continuing."
        else:
            print "Aborted."
            return None
            sys.exit(0)
        dlg.Destroy()


    #more function code...
    print "Success!"
1

1 Answers

0
votes

You cannot have 2 wxPython main loops running at the same time. That will cause some pretty screwy behavior. Personally I think I would split this code into 2 threads. When the first one finishes, it sends a message using wx.CallAfter and pubsub or wx.PostEvent and at that time you can do your if statement in a wxPython handler.

If you continue, then you spin up a second thread with the rest of the function code.