import tkinter as tk from tkinter import messagebox from controller.MainController import MainController class MainWindow(tk.Tk): """ View - MainWindow TODO dec... @author Jérémi Nihart / EndMove @link https://git.endmove.eu/EndMove/WebPicDownloader @version 1.0.1 @since 2022-09-04 """ # Variables __controller: MainController = None __views: dict = None __frame_id: int = None # Constructor def __init__(self, controller: MainController) -> None: """ Constructor * :controller: -> The main application cpntroller. """ super().__init__() # Init view repository self.__views = {} # Save and setup main controller self.__controller = controller controller.set_view(self) # Init view components & more self.__init_window() self.__init_top_menu() self.__init_bind_protocol() # START Internal methods def __init_window(self) -> None: """ [internal function] => Initialize window parameters """ # self.title('My tkinter app') # self.geometry('450x250') self.resizable(False, False) # self.config(bg='#f7ef38') def __init_top_menu(self) -> None: """ [internal function] => Initialize top menu of the window. """ main_menu = tk.Menu(self) col1_menu = tk.Menu(main_menu, tearoff=0) col1_menu.add_command(label="Open app folder", command=self.__controller.on_open_folder) col1_menu.add_separator() col1_menu.add_command(label="Quit", command=self.__controller.on_quite) main_menu.add_cascade(label="File", menu=col1_menu) 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) 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) # END Internal methods # START App methods def add_view(self, frame, view) -> None: """ [function for app] * :frame: -> the frame id of the view to add. """ self.__views[frame] = view # END App methods # START Controller methods 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) -> None: """ [function for app & controller] => Allows to display the selected frame provided that it has been previously added to the frame dictionary. * :frame: -> the frame if of the view to display. """ 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 else: raise ValueError("Unable to find the requested Frame") def close_window(self) -> None: """ [function for controller] => Closes the main window and stops the program from the controller. """ self.destroy() def show_question_dialog(self, title: str, message: str, icon: str='question') -> bool: """ [function for controller] => TODO DESC * :message: -> * RETURN -> """ return messagebox.askquestion(title, message, icon=icon) def show_information_dialog(self, title: str, message: str, icon: str='information') -> None: """ [function for controller] => TODO DESC * :message: -> * RETURN -> """ messagebox.showinfo(self, title, message, icon=icon) # END Controller methods