1
from tkinter import * 
from tkinter.ttk import *
import threading
import numpy as np
def bar():
    #function to display progress
    pass
with open('/home/user154/test.txt') as f:
    total=f.read().split('\n')
root = Tk()
links = list(filter(None, total)) #16530 links
threads =[]
#creating chunks of length 10
chunks = [i.tolist() for i in np.array_split(links, 10) if i.size>0]
# Progress bar widget 
progress = Progressbar(root, orient = HORIZONTAL, 
              length = len(links), mode = 'determinate') 
#launching the threads
for lst in chunks:
    threads.append(threading.Thread(target=download_image, args=(lst)))
for x in threads:
   x.start()
for x in threads:
    x.join()


mainloop()

Hello, How can I Update the progress bar based on threads completion. I.e I have 16530 image links which I am trying to download and want to create a Progressbar which shows the progress of downloading.

The download_image function is pretty long so I didn't include in question but basically it's requests and write to file.

martineau
  • 119,623
  • 25
  • 170
  • 301
hadesfv
  • 386
  • 4
  • 18
  • See my answer to [Freezing/Hanging tkinter Gui in waiting for the thread to complete](https://stackoverflow.com/questions/53696888/freezing-hanging-tkinter-gui-in-waiting-for-the-thread-to-complete). – martineau Mar 08 '20 at 19:17
  • @martineau thanks this will help a lot, this will display threads as a bar right? I mean in case I have 13 threads your code will show bar of length 13 – hadesfv Mar 08 '20 at 19:25
  • Yes, if you set up the progress bar and update it properly. Your threads will need to periodically put something the `Queue` for the GUI part's `processIncoming()` method to get and use to update the progress bar. A _really_ important thing to keep in mind is making sure the threads themselves do not attempt to access or update the GUI. – martineau Mar 08 '20 at 19:32
  • @martineau I see, would it be easier to calculate the number of files downloaded and use that number to update `Queue` (each thread will update `Queue`), and use only `processIncoming()` to update progress bar ? – hadesfv Mar 08 '20 at 19:37
  • Sounds like a viable simplification. The ideal would be to calculate the total number of bytes in all the files being downloaded and the make the progress bar show how much of that grand total has been completed so far — but I don't know if that's feasible. – martineau Mar 08 '20 at 19:42
  • @martineau this sounds even better, but how can I get that grand total number pre-requesting all images – hadesfv Mar 08 '20 at 19:48
  • You've got to get the metadata for the files from somewhere to do it. As I said earlier, the ideal might not be feasible to implement (i.e. if you don't have and cannot get the needed information). – martineau Mar 08 '20 at 20:59
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/209326/discussion-between-hadesfv-and-martineau). – hadesfv Mar 09 '20 at 16:42

0 Answers0