2

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

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 中运行

4

1 回答 1

1

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

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

具体看@workerthread.executeInGUIThreadDecorator,examples\example_gui_class.py中的例子

于 2012-12-15T23:22:55.227 回答