Advertisement
plarmi

download tkinter

Dec 23rd, 2023
516
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.90 KB | None | 0 0
  1. import threading
  2. import tkinter as tk
  3. from tkinter import ttk
  4. from urllib.request import urlopen
  5. def download_file_worker():
  6.     url = "https://www.python.org/ftp/python/3.7.2/python-3.7.2.exe"
  7.     filename = "python-3.7.2.exe"
  8.     # Открываем адрес URL.
  9.     with urlopen(url) as r:
  10.         with open(filename, "wb") as f:
  11.             # Чтение удаленного файла и запись локального файла.
  12.             f.write(r.read())
  13. def schedule_check(t):
  14.     """
  15.    Планирование выполнения функции `check_if_done()` в течение одной секунды.
  16.    """
  17.     root.after(1000, check_if_done, t)
  18. def check_if_done(t):
  19.     # Если поток закончился, сбросим кнопку и выведем сообщение.
  20.     if not t.is_alive():
  21.         info_label["text"] = "Файл загружен!"
  22.         # Сброс кнопки.
  23.         download_button["state"] = "normal"
  24.     else:
  25.         # Если нет, проверим еще раз через некоторое время.
  26.         schedule_check(t)
  27. def download_file():
  28.     info_label["text"] = "Загрузка файла..."
  29.     # Отключение кнопки на время загрузки файла.
  30.     download_button["state"] = "disabled"
  31.     # Запустим загрузку в новом потоке.
  32.     t = threading.Thread(target=download_file_worker)
  33.     t.start()
  34.     # Начнем периодически проверять, закончился ли поток.
  35.     schedule_check(t)
  36. root = tk.Tk()
  37. root.title("Загрузка файла с помощью Tcl/Tk")
  38. info_label = ttk.Label(text="Нажмите кнопку , чтобы загрузить файл.")
  39. info_label.pack()
  40. download_button = ttk.Button(text="Скачать файл", command=download_file)
  41. download_button.pack()
  42. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement