在这个程序中,当我在线程中使用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()
.我会考虑在循环中添加睡眠.
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运行正常.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。