2017-09-01 53 views
0

我已经编写了一个程序,它循环执行一些给定的文件(实际上是电影文件),搜索该文件的字幕并下载它。这是一个wxPython GUI应用程序。现在,每次下载字幕时,我想用一些文本更新GUI的ListControl。代码如下:用于更新wxPython GUI的thread.join()的任何替代方法?

for i in range(total_items): 
    path = self.list.GetItem(i, 0) # gets the item (first column) from list control 
    path = path.GetText() # contains the path of the movie file 

    t = Thread(target = self.Downloader, args = (path,)) 
    t.start() 

    t.join() # using this hangs the GUI and it can't be updated 
    self.list.SetItem(i, 1, 'OK') # update the listcontrol for each download 

很明显,我只想在线程完成时更新GUI。我使用了t.join(),但后来才明白它不能用于更新GUI,如此问题中所述:GObject.idle_add(), thread.join() and my program hangs

现在,我想使用类似的问题,但我不能在我使用wxPython的时候找出任何东西。

回答

1

要使用wx.CallAfter从你的线程内,将使用主线程调用无论你将它传递

def Downloader(a_listbox,item_index): 
    path = a_listbox.GetItem(item_index, 0) # gets the item (first column) from list control 
    path = path.GetText() # contains the path of the movie file 
    # do some work with the path and do your download 
    wx.CallAfter(a_listbox.SetItem, i, 1, 'OK') # update the listcontrol for each download (wx.CallAfter will force the call to be made in the main thread) 

... 
for i in range(total_items):  
    t = Thread(target = self.Downloader, args = (self.list,i,)) 
    t.start() 

根据您在下面的澄清(您还是应该使用wx.CallAfter)

def Downloader(self): 
    for item_index in range(total_items): 
     path = self.list.GetItem(item_index, 0) # gets the item (first column) from list control 
     path = path.GetText() # contains the path of the movie file 
     # do some work with the path and do your download 
     wx.CallAfter(self.list.SetItem, i, 1, 'OK') # update the listcontrol for 

Thread(target=self.Downloader).start() 
+0

对不起,但这不适用于我的情况。我的程序使用API​​密钥使用“xmlrpc”请求,并且一次只能从一个用户帐户发出一个请求。但是,您提供的代码一次运行多个请求,从而产生错误。看来,我必须找到一种方法来一个接一个地运行线程,而不是一次运行所有线程。 –

+0

...你错过了点...无论如何看到编辑后 –

+1

谢谢,它的工作。其实,我是线程新手,抱歉没有明白。 –

相关问题