wxpython textctrl disappears while processing data

210 views Asked by At

I have a really weird issue that I have been trying to solve for some time now without any luck.

My program consists of a main frame, a menu bar, underneath the menu bar resides a text control in read only mode, under that a status bar.

within my main class I have a number of functions, one is listed below. On a menu event a function associated with that menu event is called. The function below should print the name of a file that is about to be processed to the textctrl, then the file should be processed, then it moves on to the next file to be printed to textctrl then process that file etc...

Instead the textctrl box & the statusbar completely disappear whilst all the files are being processed. After processing has finished it re-appears with all the text printed to it.

I'm not sure why it disappears. I've moved the code around within the function in many different ways to try & solve the issue but to no avail.

Any help would be much appreciated.

---EDIT--- CODE REMOVED

1

There are 1 answers

2
pedram On BEST ANSWER

Sounds like what is happening is that your process is busy doing work and so it doesn't relinquish any cycles to the GUI to refresh.

What you will need to do is move the busy process out to its own thread.

You will want to do something like

import threading

class Encrypt(threading.Thread):
    def __init__(self, threadNum, asset, window):
        threading.Thread.__init__(self)
        self.threadNum = threadNum
        self.window = window
        self.signal = True

    def run(self):
        while self.signal:
            do_stuff_that_is_cpu_intensive
            for self.path in self.paths: #etc..

and in your main wx.Frame class:

class Frame(wx.Frame):
def __init__(self, *args, **kwargs):
    super(Frame, self).__init__(*args, **kwargs)

    self.InitUI()
    self.Show()
    self.count = 0 # simple thread counter. 
    self.threads = [] # this lets you iterate over your threads if you ever need to

def OnEncrypt(self):
    self.count += 1
    thread = Encrypt(self.count, asset, self)
    self.threads.append(thread)
    thread.start()

See a much more detailed method and explanation here: http://www.blog.pythonlibrary.org/2010/05/22/wxpython-and-threads/