在线程中使用while循环时出现wxPython Pango错误

在这个程序中,当我在线程中使用while循环时出现错误.没有循环我没有错误.当然在实际程序中我不会连续更新标签.知道我做错了什么吗?

这是该计划:

import wx
import thread

class Example(wx.Frame):

    def __init__(self,parent): 
        wx.Frame.__init__(self,parent)
        self.InitUI()

    def InitUI(self):
        self.SetSize((250,200))
        self.Show(True)

        self.text = wx.StaticText(self,label='',pos=(20,30))

        thread.start_new_thread(self.watch,(self,None))

    def watch(self,dummy,e):
        while True:
            self.text.SetLabel('Closed')


def main():
    ex = wx.App()
    Example(None)
    ex.MainLoop()    

if __name__ == '__main__':
    main()

这是错误

Pango:ERROR:/build/pango1.0-LVHqeM/pango1.0-1.30.0/./pango/pango-            layout.c:3801:pango_layout_check_lines: assertion Failed: (!layout->log_attrs) Aborted

关于我做错了什么的任何建议?我(显然)是线程新手.

解决方法

我不确定这是否是导致问题的原因,但是……你不应该从另一个线程与GUI交互.你应该使用 wx.CallAfter().我会考虑在循环中添加睡眠.

wx.CallAfter()文件说:

Call the specified function after the current and pending event handlers have been completed. This is also good for making GUI method calls from non-GUI threads. Any extra positional or keyword args are passed on to the callable when it is called.

更新的代码将是:

import wx
import thread
import time

class Example(wx.Frame):
    def __init__(self,e):
        while True:
            time.sleep(0.1)
            wx.CallAfter(self.text.SetLabel,'Closed')

def main():
    ex = wx.App()
    Example(None)
    ex.MainLoop()    

if __name__ == '__main__':
    main()

也许你也可以考虑使用wx.Timer.

顺便说一句:你的代码在我的电脑上使用Windows 7和wxPython 2.8运行正常.

相关文章

功能概要:(目前已实现功能)公共展示部分:1.网站首页展示...
大体上把Python中的数据类型分为如下几类: Number(数字) ...
开发之前第一步,就是构造整个的项目结构。这就好比作一幅画...
源码编译方式安装Apache首先下载Apache源码压缩包,地址为ht...
前面说完了此项目的创建及数据模型设计的过程。如果未看过,...
python中常用的写爬虫的库有urllib2、requests,对于大多数比...