Python: Threading with wxPython

92 Views Asked by At

I am new to threading and its something I am trying to get a grip of. I am creating a thread to handle a long running process separate from the main thread (which handles the graphical user process). Otherwise, I have a blocking GUI which is not nice.

The process keeps repeating itself, that the code in the thread runs again instead of stopping from the allDone function, which doesn't make sense to me.

No matter which route the code takes within the run portion (whether the findDomains function, searchAndScrape function, or the emailFormat function), they all end up at the emailFormat function calling the allDone function of MyClass which is an object of the GUI(called WindowClass):

class windowClass(wx.Frame):
    def __init__(self, parent, title):
        super(windowClass, self).__init__(parent, title=title, size=(500, 364),   style=wx.DEFAULT_FRAME_STYLE & ~wx.MAXIMIZE_BOX ^ wx.RESIZE_BORDER)
        self.SetBackgroundColour('white')
        self.basicGUI()

def allDone(self, event):
    myClass.worker.stop()
    time.sleep(2)
    dlg = wx.MessageBox("All done!", "Ask Alfred", wx.OK | wx.ICON_INFORMATION)
    if dlg.ShowModal() == wx.ID_OK:
        while True:
            try:
                os.unlink(self.fpath)
                os.rename(self.temp, self.fpath)
                self.Destroy()
            except WindowsError:
                myClass.closeIt(self)
            break

To keep the brevity and not post 500 lines of code, here is just my thread class:

class openExcel(Thread):

def __init__(self, file):
    Thread.__init__(self)
    super(openExcel, self).__init__()
    self._stop=Event()
    self.file = file
    self._want_abort = 0
    self.start()
    self._stop=False

def run(self):
    rbook=xlrd.open_workbook(self.file)
    sheet = rbook.sheet_by_index(0)

    numrows = (sheet.nrows)
    wbook = copy(rbook)
    skipNorbertvalue=myClass.skipNorbert.IsChecked()

    searchrow=numrows
    for row in range(numrows):
        valueone=sheet.cell_value(row, 2)
        if valueone =="":
            searchrow=row
            break

    scraperow = numrows
    for row in range(numrows):
        valuetwo=sheet.cell_value(row, 3)
        if valuetwo == "":
            scraperow=row
            break
        elif valuetwo=="Over search limit":
            scraperow=row
            break
    if searchrow < numrows:
        while True:
            try:
                browserindex=myClass.currentbrowser
                search=startSearch(self.file)
                search.findDomains(browserindex, searchrow, numrows, sheet, wbook, self.file)
            except AttributeError:
                myClass.noBrowser(self)
    elif skipNorbertvalue==False and scraperow < numrows:
        try:
            browserindex=myClass.currentbrowser
            search=startSearch(self.file)
            search.searchAndscrape(browserindex, scraperow, numrows, wbook, self.file)
        except AttributeError:
            myClass.noBrowser(self)
    else:
        checkformat=emailFormat()
        checkformat.possibleEmail(numrows, sheet, wbook, self.file)

def abort(self):
    self._want_abort = 1

def stop(self):
    self._stop=True
0

There are 0 best solutions below