Skip to main content
 首页 » 编程设计

python之wx.lib.editor 中的线程

2025年05月04日9zhenyulu

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

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

请您参考如下方法:

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

我为此创建了一个小型图书馆:https://github.com/vaal12/workerthread

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