开发者

Threads, wxpython and statusbar

开发者 https://www.devze.com 2023-02-04 14:28 出处:网络
I\'m doing a program in which I\'m using a wxStatusBar, when a download starts I start a child thread like this:

I'm doing a program in which I'm using a wxStatusBar, when a download starts I start a child thread like this:

def OnDownload(self, event):
    child = threading.Thread(target=self.Download)
    child.setDaemon(True)
    child.start()

Dow开发者_如何学Gonload is another function without parameters (except self). I would like to update my statusbar from there with some information about the downloading progress, but when I try to do so I often get Xwindow, glib and segfaults errors. Any idea to solve this?

Solved: I just needed to include wx.MutexGuiEnter() before changing something in the GUI inside the thread and wx.MutexGuiLeave() when finished. For example

def Download(self):
    #stuff that doesn't affect the GUI
    wx.MutexGuiEnter()
    self.SetStatusText("This is a thread")
    wx.MutexGuiLeave()

And that's all :D


Most people get directed to the wxPython wiki:

http://wiki.wxpython.org/LongRunningTasks

I also wrote up a little piece on the subject here:

http://www.blog.pythonlibrary.org/2010/05/22/wxpython-and-threads/

I don't think I've ever seen your solution before though.


How are you updating the status bar?

I think you should be fine if you create a custom event, and then post it via wx.PostEvent to notify the frame/status bar in the GUI thread.

For download progress in a status bar, you might want your event to look something like this:

DownloadProgressEvent, EVT_DL_PROGRESS = wx.lib.newevent.NewEvent()

# from the thread...

event = DownloadProgressEvent(current=100, total=1000, filename="foo.jpg")
wx.PostEvent(frame, event)

# from the frame:

def OnDownloadProgress(self, event):
    self.statusbar.update_dl_msg(event.current, event.total, event.filename)

Here's some more detail from the wxPython wiki.

0

精彩评论

暂无评论...
验证码 换一张
取 消