Search code examples
databasetextcontrolswxpythonlarge-data-volumes

wxpython: Is there a quicker method for inserting large quantities of text into text controls?


I'm coding a GUI in wxpython right now and one of its features is a text control. This text control often takes 10's of thousands of numbers of varying length inserted into it. When it is being filled with the data, it takes a long time (30 seconds or more perhaps).

Just wondering, is there a method to filling the text control with data that will make it do it quicker? Thanks.


Solution

  • I suppose it rather depends on whether the delay is due to the act of getting the numbers or the loading of the text control.
    If it is the text control, you can pre-load a variable with the data and then load it in one hit.

    import wx
    import time
    class test(wx.Frame):
        def __init__(self):
            wx.Frame.__init__(self, None)
            self.panel = wx.Panel(self, wx.ID_ANY)
            self.tc = wx.TextCtrl(self.panel, wx.ID_ANY, size=(300,400),
                            style = wx.TE_MULTILINE|wx.TE_READONLY|wx.VSCROLL)
            text=""
            start = time.time()
            for i in range(1,30000):
                text+=str(i)+'\n'
    #            self.tc.AppendText(str(i)+"\n")
            self.tc.WriteText(text)
            self.Show()
            end = time.time()
            print (end - start)
    
    if __name__ == '__main__':
        app = wx.App()
        frame = test()
        app.MainLoop()
    

    Here, I am building text with the numbers and then loading it once, with WriteText.
    If you comment out the text=text+str(i)+'\n' and self.tc.WriteText(text) lines and uncomment the self.tc.AppendText(str(i)+"\n") line which loads the text control one number at a time, you should see that the first method runs multiple times faster. At least it does on my box.