wxPython如何在另一个进程发生时显示加载条?

2024-09-28 21:55:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在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


Tags: the函数代码inselfcomhttp文章
1条回答
网友
1楼 · 发布于 2024-09-28 21:55:35

您需要从正在进行抓取的线程向主应用程序发送消息,告诉它更新进度条。这意味着您必须使用wxPython线程安全方法之一:

  • 在wx.CallAfter公司在
  • 在wx.CallLater公司在
  • 在wx.事件后在

我认为最简单的方法之一就是结合使用pubsub和CallAfter。可以使用pubsub将消息发布到需要侦听器的对话框。你可以在这里阅读如何做到这一点:http://www.blog.pythonlibrary.org/2010/05/22/wxpython-and-threads/

更新:

下面是一个示例应用程序,演示如何从线程定期更新gauge小部件(与wxPython 2.8一起使用):

import time
import wx

from threading import Thread

from wx.lib.pubsub import Publisher

########################################################################
class TestThread(Thread):
    """Test Worker Thread Class."""

    #                                   
    def __init__(self):
        """Init Worker Thread Class."""
        Thread.__init__(self)
        self.start()    # start the thread

    #                                   
    def run(self):
        """Run Worker Thread."""
        # This is the code executing in the new thread.
        for i in range(20):
            time.sleep(1)
            wx.CallAfter(Publisher().sendMessage, "update", "")

########################################################################
class MyProgressDialog(wx.Dialog):
    """"""

    #                                   
    def __init__(self):
        """Constructor"""
        wx.Dialog.__init__(self, None, title="Progress")
        self.count = 0

        self.progress = wx.Gauge(self, range=20)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.progress, 0, wx.EXPAND)
        self.SetSizer(sizer)

        # create a pubsub receiver
        Publisher().subscribe(self.updateProgress, "update")

    #                                   
    def updateProgress(self, msg):
        """"""
        self.count += 1

        if self.count >= 20:
            self.Destroy()

        self.progress.SetValue(self.count)


########################################################################
class MyForm(wx.Frame):

    #                                   
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "Tutorial")

        # Add a panel so it looks the correct on all platforms
        panel = wx.Panel(self, wx.ID_ANY)
        self.btn = btn = wx.Button(panel, label="Start Thread")
        btn.Bind(wx.EVT_BUTTON, self.onButton)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(btn, 0, wx.ALL|wx.CENTER, 5)
        panel.SetSizer(sizer)

    #                                   
    def onButton(self, event):
        """
        Runs the thread
        """
        btn = event.GetEventObject()
        btn.Disable()

        TestThread()
        dlg = MyProgressDialog()
        dlg.ShowModal()

        btn.Enable()


#                                   
# Run the program
if __name__ == "__main__":
    app = wx.PySimpleApp()
    frame = MyForm().Show()
    app.MainLoop()

如果您使用的是wxpython2.9,那么pubsub已经更新为使用新的pubsubapi。例如w9.xpythan

^{pr2}$

相关问题 更多 >