在wx.lib.editor中进行线程处理

时间:2012-11-22 04:10:07

标签: python multithreading editor wxpython

我在使用线程和编辑器控件时遇到问题,我无法在编辑器中使用线程进程。 例如,我只是简单地将文本添加到带有线程的编辑器控件中,但它发生了错误:

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

这是我的代码:

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()

请帮我修理一下。我使用wx.python库,python 2.7,在窗口7中运行

1 个答案:

答案 0 :(得分:0)

当您尝试在非GUI线程中更新GUI小部件时,通常会遇到此类错误。

我为此目的制作了一个小型图书馆:https://github.com/vaal12/workerthread

特别关注@ workerthread.executeInGUIThreadDecorator,示例中的示例\ example_gui_class.py