python - wx.lib.editor 中的线程

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

最佳答案

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

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

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

关于python - wx.lib.editor 中的线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13505776/

相关文章:

python - Numpy 和 PyTables 的浮点异常

python - Django - 在外键模型的模型上设置默认图像

multithreading - Delphi:FormStyle:fsStayOnTop.InputQuery 在线程中并且位于表单后面

c# - 线程的执行上下文

java - 一段时间内出现网络问题时,客户端与远程 Hbase 服务器的连接

vim - 在 Vim 中,ctrl+w ctrl+w 的反义词是什么?

editor - Aptana 开放资源 key 绑定(bind)

python - 类中未定义/未实现属性的 Python "catch all"方法

python - 无法在 Kaggle 内核中使用 'kaggle.competitions'

javascript - Sublime Text - 在 apache tomcat 上运行项目,无需刷新 eclipse 项目