Download progressbar for Python 3

前端 未结 2 1410
鱼传尺愫
鱼传尺愫 2020-12-05 20:26

I need a progress to show during file download for Python 3. I have seen a few topics on Stackoverflow, but considering that I\'m a noob at programming and nobody posted a

2条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-05 20:43

    I think this piece of code can help you. I'm not quite sure it's exactly what you want. At least it should give you something to work on.

    import tkinter 
    from tkinter import ttk
    from urllib.request import urlopen
    
    
    def download(event):
        file = urlopen('http://www.python.org/')
        output = open('downloaded_file.txt', 'wb')
        lines= file.readlines()
        i = len(lines)
    
        for line in lines:
            output.write(line)
            pbar.step(100/i)
    
        output.close()
        file.close()
    
    
    
    
    root = tkinter.Tk()
    root.title('Download bar')
    
    pbar = ttk.Progressbar(root, length=300)
    pbar.pack(padx=5, pady=5)
    
    btn = tkinter.Button(root, text="Download")
    # bind to left mouse button click
    btn.bind("", download)
    btn.pack(pady=10)
    
    root.mainloop()
    

    This works, I've tried it.

提交回复
热议问题