我正在wxPython中创建一个RSS提要聚合器客户端,我尝试实现的一个函数是刷新提要并显示任何新文章的刷新函数。然而,每次调用刷新函数时,所有的web文章都必须被再次刮取并显示在屏幕上,这通常需要6-7秒。所以我创造了一个wx.对话框用一个持续7秒的负荷计上课。我的目的是在所有文章都被刮掉的时候用量规显示这个对话框。我试图用线程模块来做这个,但是没有用。直到仪表启动,对话才开始。以下是当前代码:
def OnRefresh(self, e): #Reloads the scraper module and mimics previous processes, replaces the old articles in the list with the new articles
gauge = LoadingGauge(None, title='', size=(300,200))
threading.Thread(target=gauge.InitUI()).start() #Show the loading screen while the main program is refreshing (But it doesn't...)
reload(scraper)
self.webpage = scraper.Scraper('http://feeds.huffingtonpost.com/huffingtonpost/LatestNews')
self.statusBar.SetStatusText('Refreshing feed...')
self.webpage.scrape()
self.statusBar.SetStatusText('')
self.listCtrl.DeleteAllItems()
for i in range(0, 15):
self.listCtrl.InsertStringItem(i, self.webpage.titles[i])
self.browser.LoadURL(self.webpage.links[0])
self.Layout()
如果有人能发现我的错误或者重新给我一个新的解决方案,那就太好了。 完整的代码可以在这里找到:https://github.com/JackSac67/Huffeeder,刷新工具使用的图标可以在这里找到:http://files.softicons.com/download/internet-cons/feedicons-2-icons-by-zeusbox-studio/png/32/reload.png
您需要从正在进行抓取的线程向主应用程序发送消息,告诉它更新进度条。这意味着您必须使用wxPython线程安全方法之一:
我认为最简单的方法之一就是结合使用pubsub和CallAfter。可以使用pubsub将消息发布到需要侦听器的对话框。你可以在这里阅读如何做到这一点:http://www.blog.pythonlibrary.org/2010/05/22/wxpython-and-threads/
更新:
下面是一个示例应用程序,演示如何从线程定期更新gauge小部件(与wxPython 2.8一起使用):
如果您使用的是wxpython2.9,那么pubsub已经更新为使用新的pubsubapi。例如w9.xpythan
^{pr2}$相关问题 更多 >
编程相关推荐