This repository has been archived on 2023-11-29. You can view files and clone it, but cannot push or open issues or pull requests.
WebPicDownloader/webpicdownloader/view/MainWindow.py

155 lines
5.3 KiB
Python
Raw Permalink Normal View History

import tkinter as tk
2022-09-04 17:38:45 +02:00
from tkinter import messagebox
2022-09-11 11:52:41 +02:00
from webpicdownloader.controller.Frames import Frames
from webpicdownloader.controller.MainController import MainController
2022-08-30 21:12:04 +02:00
class MainWindow(tk.Tk):
"""
2022-08-30 21:12:04 +02:00
View - MainWindow
This view is the main view of the application, it manages the different frames/views
of the application, captures the events to send them to the main controller.
2022-08-30 21:12:04 +02:00
@author Jérémi Nihart / EndMove
@link https://git.endmove.eu/EndMove/WebPicDownloader
2022-09-04 17:38:45 +02:00
@version 1.0.1
@since 2022-09-04
"""
# Variables
2022-08-30 13:51:50 +02:00
__controller: MainController = None
__views: dict = None
2022-08-31 12:07:43 +02:00
__frame_id: int = None
# Constructor
def __init__(self, controller: MainController) -> None:
2022-09-04 17:38:45 +02:00
"""
Constructor
* :controller: -> The main application cpntroller.
"""
super().__init__()
2022-08-30 13:51:50 +02:00
# Init view repository
self.__views = {}
2022-08-31 12:07:43 +02:00
# Save and setup main controller
self.__controller = controller
controller.set_view(self)
# Init view components & more
2022-08-31 12:07:43 +02:00
self.__init_window()
self.__init_top_menu()
self.__init_bind_protocol()
2022-08-31 12:07:43 +02:00
# START Internal methods
def __init_window(self) -> None:
2022-08-31 12:07:43 +02:00
"""
[internal function]
=> Initialize window parameters
"""
2022-09-11 11:52:41 +02:00
self.iconbitmap(f"{self.__controller.get_config('sys_directory')}\\webpicdownloader\\assets\\logo.ico") # App logo
window_width = 430 # App width
window_height = 305 # App height
x_cordinate = int((self.winfo_screenwidth()/2) - (window_width/2))
y_cordinate = int((self.winfo_screenheight()/2) - (window_height/2))
self.geometry(f"{window_width}x{window_height}+{x_cordinate}+{y_cordinate}") # App size and middle centering
self.resizable(False, False) # Disable app resizing
2022-08-31 12:07:43 +02:00
def __init_top_menu(self) -> None:
2022-08-31 12:07:43 +02:00
"""
[internal function]
=> Initialize top menu of the window.
"""
main_menu = tk.Menu(self)
2022-08-30 21:12:04 +02:00
# Top menu File item
2022-08-31 12:07:43 +02:00
col1_menu = tk.Menu(main_menu, tearoff=0)
2022-09-04 17:38:45 +02:00
col1_menu.add_command(label="Open app folder", command=self.__controller.on_open_folder)
2022-08-31 12:07:43 +02:00
col1_menu.add_separator()
col1_menu.add_command(label="Quit", command=self.__controller.on_quite)
2022-08-31 12:07:43 +02:00
main_menu.add_cascade(label="File", menu=col1_menu)
# Top menu Help item
2022-08-31 12:07:43 +02:00
col2_menu = tk.Menu(main_menu, tearoff=0)
col2_menu.add_command(label="Check for update", command=self.__controller.on_check_for_update)
col2_menu.add_command(label="About", command=self.__controller.on_about)
2022-08-31 12:07:43 +02:00
main_menu.add_cascade(label="Help", menu=col2_menu)
self.config(menu=main_menu)
def __init_bind_protocol(self) -> None:
"""
[internal function]
=> Initialize the function bindding on events of the main window.
"""
self.protocol("WM_DELETE_WINDOW", self.__controller.on_quite)
2022-08-31 12:07:43 +02:00
# END Internal methods
2022-08-30 21:12:04 +02:00
# START App methods
2022-09-04 17:38:45 +02:00
def add_view(self, frame, view) -> None:
2022-08-30 13:51:50 +02:00
"""
[function for app]
2022-09-04 17:38:45 +02:00
* :frame: -> the frame id of the view to add.
2022-08-30 13:51:50 +02:00
"""
self.__views[frame] = view
2022-08-30 21:12:04 +02:00
# END App methods
2022-08-30 21:12:04 +02:00
# START Controller methods
2022-09-04 17:38:45 +02:00
def set_window_title(self, title: str) -> None:
"""
[function for controller]
=> Sets the title of the main window.
* :title: -> Window title.
"""
self.title(title)
def show_frame(self, frame: Frames) -> None:
2022-08-30 13:51:50 +02:00
"""
[function for app & controller]
2022-08-31 12:07:43 +02:00
=> Allows to display the selected frame provided that it
has been previously added to the frame dictionary.
2022-08-30 13:51:50 +02:00
2022-09-04 17:38:45 +02:00
* :frame: -> the frame if of the view to display.
2022-08-30 13:51:50 +02:00
"""
2022-08-31 12:07:43 +02:00
if self.__views.get(frame):
if self.__frame_id:
self.__views.get(self.__frame_id).pack_forget()
self.__views.get(frame).pack(fill=tk.BOTH, expand=False)
self.__frame_id = frame
2022-08-30 13:51:50 +02:00
else:
2022-08-30 21:12:04 +02:00
raise ValueError("Unable to find the requested Frame")
def close_window(self) -> None:
"""
2022-09-04 17:38:45 +02:00
[function for controller]
=> Closes the main window and stops the program from the controller.
"""
self.destroy()
2022-09-04 17:38:45 +02:00
def show_question_dialog(self, title: str, message: str, icon: str='question') -> bool:
"""
[function for controller]
=> Display a question dialog to the user, which he can answer with yes or no.
2022-09-04 17:38:45 +02:00
* :title: -> Title of the dialogue.
* :message: -> Message of the dialogue displayed to the user.
* :icon: -> Icon of the dialogue displayed to the user.
* RETURN -> True id the user selected yes, False else.
2022-09-04 17:38:45 +02:00
"""
return True if (messagebox.askquestion(title, message, icon=icon) == "yes") else False
2022-09-04 17:38:45 +02:00
def show_information_dialog(self, title: str, message: str, icon: str='info') -> None:
2022-09-04 17:38:45 +02:00
"""
[function for controller]
=> Display an information dialog to the user.
2022-09-04 17:38:45 +02:00
* :title: -> Title of the dialogue.
* :message: -> Message of the dialogue displayed to the user.
* :icon: -> Icon of the dialogue displayed to the user.
2022-09-04 17:38:45 +02:00
"""
messagebox.showinfo(title, message, icon=icon)
2022-08-31 12:07:43 +02:00
# END Controller methods