fixing bugs with the textarea and background task

This commit is contained in:
Jérémi N ‘EndMove’ 2022-09-01 11:47:34 +02:00
parent 03e35c18a9
commit 5c6609ff0a
Signed by: EndMove
GPG Key ID: 65C4A02E1F5371A4
3 changed files with 59 additions and 7 deletions

View File

@ -1,5 +1,5 @@
import time
from controller.MainController import MainController from controller.MainController import MainController
from util.AsyncTask import AsyncTask
class HomeController: class HomeController:
@ -20,6 +20,12 @@ class HomeController:
# Constructor # Constructor
def __init__(self, controller: MainController, webpic) -> None: def __init__(self, controller: MainController, webpic) -> None:
"""
Constructor
:controller: -> The main application cpntroller.
:webpic: -> The webpicdownloader instance.
"""
self.__main_controller = controller self.__main_controller = controller
self.__webpic = webpic self.__webpic = webpic
@ -27,6 +33,7 @@ class HomeController:
def set_view(self, view) -> None: def set_view(self, view) -> None:
""" """
[function for view] [function for view]
=> Define the view of this controller.
:view: -> The view that this controller manage. :view: -> The view that this controller manage.
""" """
@ -38,6 +45,7 @@ class HomeController:
def on_change_view(self, frame) -> None: def on_change_view(self, frame) -> None:
""" """
[event function for view] [event function for view]
=> Call this event method when the user requests to change the window.
:frame: -> The frame we want to launch. :frame: -> The frame we want to launch.
""" """
@ -49,19 +57,22 @@ class HomeController:
def on_download_requested(self, url: str, name: str) -> None: def on_download_requested(self, url: str, name: str) -> None:
""" """
[event function for view] [event function for view]
=> Call this event method when the user requests to download
:url: -> The url of the website to use for pic-download.\n :url: -> The url of the website to use for pic-download.\n
:name: -> The name of the folder in which put pictures. :name: -> The name of the folder in which put pictures.
""" """
if url.strip() and name.strip() : # Define the download task function (to call in a AsyncTask)
# TODO use webpic here to download files def download_task():
print(f"url : {url} -- name : {name}") self.__view.clear_logs()
self.__view.clear_logs() # BUG this process must be asynchrone ...
if self.__webpic.download(url, name): if self.__webpic.download(url, name):
self.__view.show_success_message("The download has been successfully completed.") self.__view.show_success_message("The download has been successfully completed.")
else: else:
self.__view.show_error_message("A critical error preventing the download occurred, check the logs.") self.__view.show_error_message("A critical error preventing the download occurred, check the logs.")
# Verify variable and start AsyncTask
if url.strip() and name.strip() :
AsyncTask(download_task).start()
else: else:
self.__view.show_error_message("Opss, the url or folder name are not valid!") self.__view.show_error_message("Opss, the url or folder name are not valid!")
# END View events # END View events

40
util/AsyncTask.py Normal file
View File

@ -0,0 +1,40 @@
import threading
class AsyncTask(threading.Thread):
"""
AsyncTask
AsyncTask is a utility class allowing to execute a task asynchronously in a thread.
For more informations read the constructor documentation.
@author Jérémi Nihart / EndMove
@link https://git.endmove.eu/EndMove/WebPicDownloader
@version 1.0.0
@since 2022-09-01
"""
# Variables
__callback = None
__args: list = None
# Constructor
def __init__(self, callback, args=()) -> None:
"""
Constructor
=> Spacify here the function that should be launched asynchronously. Then use the
function {AsyncTask.start()} to start the thread and the processing.
[!]: The function {AsyncTask.run()} is reserved for the thread and should not be run
from outside.
:callback: -> Is the function to launch asynchronously.
:args: -> Argument to pass to the function when executing it.
"""
super().__init__()
self.__callback = callback
self.__args = args
def run(self) -> None:
"""
[!] : This function should not be used! Start the task with {AsyncTask.start()}!
"""
self.__callback(*self.__args)

View File

@ -78,6 +78,7 @@ class HomeView(ttk.Frame):
""" """
self.log_textarea.configure(state=tk.NORMAL) self.log_textarea.configure(state=tk.NORMAL)
self.log_textarea.insert(tk.END, f"~ {line}\n") self.log_textarea.insert(tk.END, f"~ {line}\n")
self.log_textarea.see(tk.END)
self.log_textarea.configure(state=tk.DISABLED) self.log_textarea.configure(state=tk.DISABLED)
def clear_logs(self) -> None: def clear_logs(self) -> None:
@ -86,7 +87,7 @@ class HomeView(ttk.Frame):
TODO desc TODO desc
""" """
self.log_textarea.configure(state=tk.NORMAL) self.log_textarea.configure(state=tk.NORMAL)
self.log_textarea.delete("1.0", tk.END) self.log_textarea.delete('1.0', tk.END)
self.log_textarea.configure(state=tk.DISABLED) self.log_textarea.configure(state=tk.DISABLED)
def show_error_message(self, message) -> None: def show_error_message(self, message) -> None: