Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用progressbar运行urllib2调用程序崩溃_Python_Download_Tkinter_Progress Bar_Ttk - Fatal编程技术网

Python 使用progressbar运行urllib2调用程序崩溃

Python 使用progressbar运行urllib2调用程序崩溃,python,download,tkinter,progress-bar,ttk,Python,Download,Tkinter,Progress Bar,Ttk,您好,我的代码有问题,可能不是urllib2本身,但它不会创建进度条并崩溃。但是在等待代码完成后下载我的文件。因为我对python几乎没有经验,我是否可以阻止挂起并可能将下载分解成更小的块。。。我的代码如下: def iPod1(): pb = ttk.Progressbar(orient ="horizontal",length = 200, mode ="indeterminate") pb.pack(side="top") pb.start() download = "http://do

您好,我的代码有问题,可能不是urllib2本身,但它不会创建进度条并崩溃。但是在等待代码完成后下载我的文件。因为我对python几乎没有经验,我是否可以阻止挂起并可能将下载分解成更小的块。。。我的代码如下:

def iPod1():
pb = ttk.Progressbar(orient ="horizontal",length = 200, mode ="indeterminate")
pb.pack(side="top") 
pb.start()
download = "http://downloads.sourceforge.net/project/whited00r/7.1/Whited00r71-iPodTouch1G.zip?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fwhited00r%2Ffiles%2F7.1%2F&ts=1405674672&use_mirror=softlayer-ams"
request = urllib2.urlopen( download)
pb.start()
output = open("Whited00r71-iPodTouch1G.zip", "wb")
output.write(request.read())
output.close()
pb.stop
tkMessageBox.showinfo(title="Done", message="Download complete. Please follow the installation instructions provided in the .html file.")   

通过将下载移动到自己的线程中,可以避免挂起/冻结GUI。由于GUI不能从Tk mainloop运行所在线程以外的其他线程更改,因此我们必须定期检查下载线程是否已完成。这通常是通过在小部件对象上使用after方法重复调度延迟函数调用来完成的

使用shutil.copyfileobj可以在将整个文件写入本地文件之前不将其读取到内存中


显示完整的错误消息。有多行出现问题-请在代码中标记该行。是否在console/terminal/cmd.exe中运行了该行?当您关闭console/terminal/cmd.exe时,是否在其中收到任何消息?它是否真的永久停止?在整个下载过程中,您的代码肯定会阻止/冻结GUI,但应该在下载后继续。下载后,它会再次开始响应。是否有一种方法可以避免阻塞/冻结GUI。我知道线程技术,但我不知道如何使用它。@itechy21那不是一个按钮可以调用的函数。启动ipod1的启动下载功能是启动下载的功能。
import shutil
import tkMessageBox
import ttk
import urllib2
from threading import Thread

IPOD_1_URL = (
    'http://downloads.sourceforge.net/project/whited00r/7.1/'
    'Whited00r71-iPodTouch1G.zip'
    '?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fwhited00r%2Ffiles%2F7.1%2F'
    '&ts=1405674672'
    '&use_mirror=softlayer-ams'
)


def download(url, filename):
    response = urllib2.urlopen(url)
    with open(filename, 'wb') as output_file:
        shutil.copyfileobj(response, output_file)


def check_download(thread, progress_bar):
    if thread.is_alive():
        progress_bar.after(500, check_download, thread, progress_bar)
    else:
        progress_bar.stop()
        tkMessageBox.showinfo(
            title='Done',
            message='Download complete. Please follow the installation'
                ' instructions provided in the .html file.'
        )


def start_download_for_ipod1():
    progress_bar = ttk.Progressbar(
        orient='horizontal', length=200, mode='indeterminate'
    )
    progress_bar.pack(side='top')
    progress_bar.start()
    thread = Thread(
        target=download, args=(IPOD_1_URL, 'Whited00r71-iPodTouch1G.zip')
    )
    thread.daemon = True
    thread.start()
    check_download(thread, progress_bar)