Threading in wx.lib.editor

I have a problem with use thread and editor control, I can't use thread process in editor. For example, I just simple add text into editor control with thread, but it happen bug:

PyAssertionError: C++ assertion "m_buffer && m_buffer->IsOk()" failed at ..\..\include\wx/dcbuffer.h(104) in wxBufferedDC::UnMask(): invalid backing store

Here is my code:

import threading
import  wx
import  wx.lib.editor    as  editor

class RunTest(threading.Thread):
    def __init__(self,master,type):
        threading.Thread.__init__(self)
        self.master = master
        self.type = type

    def run(self):
        if self.type == 1:
            for i in range(1000):
                self.master.ed.BreakLine(None)
                self.master.ed.SingleLineInsert("Text Line Number: " + str(i))

class Test(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, 'Test', size=(900, 700))
        win = wx.Panel(self, -1)
        self.ed = editor.Editor(win, -1)
        box = wx.BoxSizer(wx.VERTICAL)
        box.Add(self.ed, 1, wx.ALL|wx.GROW, 1)
        win.SetSizer(box)
        win.SetAutoLayout(True)
        self.ed.Bind(wx.EVT_LEFT_DCLICK, self.OnClick)

    def OnClick(self,event):
        thread = RunTest(self,1)
        thread.start()


if __name__ == '__main__':
    app = wx.PySimpleApp()
    root = Test()
    root.Show()
    app.MainLoop()

Please help me fix it. I use wx.python library,python 2.7, run in window 7

Upvotes: 2

Views: 218

Answers (1)

Alexey Vassiliev
Alexey Vassiliev

Reputation: 2435

You usually get this kind of errors, when you try to update GUI widgets in non GUI thread.

I have made a small library for those purposes: https://github.com/vaal12/workerthread

Specifically look to @workerthread.executeInGUIThreadDecorator, example in examples\example_gui_class.py

Upvotes: 1

Related Questions