用迅雷下载WORD压缩文件,首次成,第二次下载,却经常出现PHP文件,打开不了,是啥原因
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用迅雷下载WORD压缩文件,首次成,第二次下载,却经常出现PHP文件,打开不了,是啥原因相关的知识,希望对你有一定的参考价值。
把任务栏全部删除,又能恢复下载WORD,是迅雷的问题不?
参考技术A 不是,是浏览器的问题! 参考技术B 被封为什么第二次线程就不能用了?
我是一个python的外行,我正在创建这个下载器应用程序,使用的是 tkinter和urllib.request 我建立了一个没有任何线程的程序,当我下载一个文件时,它会冻结,直到文件被下载,我不希望这样,所以我尝试了 线程和事件 而且它似乎工作得很好,但当我想下载第二个文件时,它却不能工作。为什么会出现这种情况?我做错了什么?我的代码。
from tkinter import *
from tkinter import font as tkFont
import random
import urllib.request
import requests
import threading
import wget
import queue
def printsth():
print("Yay it works! ")
def main_menu():
root = Tk()
root.title('8-bit downloader ')
root.iconbitmap(r"C:\Users\rayanravesh\PycharmProjects\GUI_Calculator\icon.ico")
root.geometry("600x300")
# the top menu
global num, chum, var
num = IntVar()
chum = IntVar()
var = IntVar()
menu = Menu(root)
root.config(menu=menu)
submenu = Menu(menu)
menu.add_cascade(label="Settings", menu=submenu)
def custom_op():
custom = Toplevel()
custom.iconbitmap(r"C:\Users\rayanravesh\PycharmProjects\GUI_Calculator\icon.ico")
submenu.add_command(label="Customization ", command=custom_op)
def settings_op():
global gps
set_win = Toplevel()
set_win.iconbitmap(r"C:\Users\rayanravesh\PycharmProjects\GUI_Calculator\icon.ico")
path_label = Label(set_win, text="Current default download path: ")
path_entry = Entry(set_win, width=30)
file_read = open('Data.txt', 'r')
data_base = file_read.read()
path_entry.insert(0, data_base)
file_read.close()
def default_output():
global location
file_read2 = open('Data.txt', 'r+')
file_read2.truncate(0)
file_read2.close()
write_file2 = open('Data.txt', 'w')
write_file2.write(path_entry.get())
write_file2.close()
location = path_entry.get() + "\\"
default_location = location.replace("\\", "\\\\")
path_btn = Button(set_win, text="Submit ", command=default_output)
path_label.pack(anchor=CENTER, expand=1)
path_entry.pack(anchor=CENTER, expand=1)
path_btn.pack(anchor=CENTER, expand=1)
submenu.add_command(label="Settings ", command=settings_op)
submenu.add_separator()
submenu.add_command(label="Exit", command=root.destroy)
# the section menu
editmenu = Menu(menu)
menu.add_cascade(label="Sections(soon)", menu=editmenu)
editmenu.add_command(label="Downloader", command=printsth)
editmenu.add_command(label="Converter", command=printsth)
editmenu.add_command(label="Media Player", command=printsth)
editmenu.add_command(label="Editor", command=printsth)
# the tool bar
toolbar = Frame(root, bg="light gray")
insert_button = Button(toolbar, text="Insert an image", command=printsth)
insert_button.pack(side=LEFT, padx=2, pady=2)
print_button = Button(toolbar, text="Print", command=printsth)
print_button.pack(side=LEFT, padx=2, pady=2)
toolbar.pack(side=TOP, fill=X)
# the download initiation
def initiate():
event.set()
# the status bar
status_bar = Label(root, text="Downloading...", bd=1, relief=SUNKEN, anchor=W)
status_bar.pack(side=BOTTOM, fill=X)
# the download frame
body_frame = Frame(root, bg="light blue")
download_button = Button(body_frame, text="Download! ", command=initiate, border=3, width=20, height=5)
download_design = tkFont.Font(size=12, slant='italic')
download_button['font'] = download_design
download_button.pack(side=LEFT, pady=5, padx=5)
body_frame.pack(side=LEFT, fill=Y)
# the main interaction menu
inter_frame = Frame(root)
global name_entry, format_entry, url_entry, output_entry
url_entry = Entry(inter_frame, width=30)
label = Label(inter_frame, text="Enter the image URL: ")
file_format = Label(inter_frame, text="Choose your file format: ")
format_entry = Entry(inter_frame, width=30)
file_name = Label(inter_frame, text="File's name: ")
name_entry = Entry(inter_frame, width=30)
check_name = Checkbutton(inter_frame, text="Give a random name", variable=num)
check_format = Checkbutton(inter_frame, text="Download with default format", variable=var)
check_default = Checkbutton(inter_frame, text="Download to default path", variable=chum)
check_default.deselect()
output_path = Label(inter_frame, text="Choose output path: ")
output_entry = Entry(inter_frame, width=30)
file_name.pack(anchor=CENTER, expand=1)
name_entry.pack(anchor=CENTER, expand=1)
check_name.pack(anchor=CENTER, expand=1)
label.pack(anchor=CENTER, expand=1)
url_entry.pack(anchor=CENTER, expand=1)
file_format.pack(anchor=CENTER, expand=1)
format_entry.pack(anchor=CENTER, expand=1)
format_entry.insert(0, '.')
check_format.pack(anchor=CENTER)
output_path.pack(anchor=CENTER, expand=1)
output_entry.pack(anchor=CENTER, expand=1)
check_default.pack(anchor=CENTER, expand=1)
inter_frame.pack(expand=1)
root.mainloop()
# the end!
def download_image():
event.wait()
while event.is_set():
print('hi')
global formatname
if num.get() == 1:
name = random.randrange(1, 1000000)
else:
name = str(name_entry.get())
formatname = str(format_entry.get())
'''if var.get() == 1:
operator = str(url_entry.get())
formatname = '.' + operator[-3] + operator[-2] + operator[-1]
else:
pass'''
fullname = str(name) + formatname
url = str(url_entry.get())
fw = open('file-size.txt', 'w')
file_size = int(requests.head(url, headers='accept-encoding': '').headers['Content-Length'])
fw.write(str(file_size))
fw.close()
path = str(output_entry.get()) + "\\"
if chum.get() == 1:
filee = open('Data.txt', 'r')
destination = filee.read()
path = destination + "\\"
output_entry.insert(0, destination)
filee.close()
else:
output_entry.delete(0, END)
urllib.request.urlretrieve(url, path.replace("\\", "\\\\") + fullname)
event = threading.Event()
t1 = threading.Thread(target=main_menu)
t2 = threading.Thread(target=download_image)
t1.start()
t2.start()
答案
我也是个外行。但我怀疑,在不了解线程的情况下,你不能连续两次使用event.set()。
我想,在你的download_image()函数的最后,你应该调用。
event.clear()
这可能不是正确的函数,但在你想再次调用它之前,关闭或释放线程在我看来是符合逻辑的。如果没有用,很抱歉。
以上是关于用迅雷下载WORD压缩文件,首次成,第二次下载,却经常出现PHP文件,打开不了,是啥原因的主要内容,如果未能解决你的问题,请参考以下文章