0

I have written a program, that loops through some given files (movie files actually), searches for subtitle for that file, and downloads it. It's a wxPython GUI application. Now, each time a subtitle is downloaded, I want to update the ListControl of the GUI with some text. The code is below:

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

It is obvious that I want to update the GUI only when the thread finishes. I used t.join(), but later understood that it cannot be used to update GUI's, as stated in this question: GObject.idle_add(), thread.join() and my program hangs

Now, I want to use something similar as stated in that question, but I can't figure anything out as I am using wxPython.

1 Answer 1

1

you want to use wx.CallAfter from inside your thread, which will use the main thread to call whatever you pass it

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()

based on your clarification below (you still should use 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()
4
  • Sorry to say, but this won't work in my case. My program uses "xmlrpc" request using an API key, and only one request can be made at a time from a single user account. But your provided code runs multiple request at once, thus generating an error. It seems like, I have to find out a way to run the threads one after another, instead of running all of them at once. Sep 1, 2017 at 18:48
  • 1
    Thanks, it worked. Actually, I am new to threading, sorry for not getting the point. Sep 1, 2017 at 19:26
  • @SadmanMuhibSamyo Just a query. If you can only download one file at a time, why are you threading the download? Is your code doing something else at the same time? Sep 2, 2017 at 6:50
  • 1. The UI stops responding till all the downloads are complete 2. I can't update the UI till all downloads are complete @rolf Sep 3, 2017 at 8:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.