Frame switching system done

This commit is contained in:
Jérémi N ‘EndMove’ 2022-08-30 21:12:04 +02:00
parent 746b40fa18
commit dce1b2f9a5
Signed by: EndMove
GPG Key ID: 65C4A02E1F5371A4
8 changed files with 241 additions and 117 deletions

View File

@ -2,5 +2,17 @@ from enum import Enum
class Frames(Enum): class Frames(Enum):
"""
Enumeration - Frames
Lists the different windows of the program in order to facilitate
their call during the execution. Each parameter of the enumeration
represents a Frame/Tab.
@author Jérémi Nihart / EndMove
@link https://git.endmove.eu/EndMove/WebPicDownloader
@version 1.0.0
@since 2022-08-30
"""
Home = 1 # Home view Home = 1 # Home view
Info = 2 # Info & copyright view Info = 2 # Info & copyright view

View File

@ -1,26 +1,50 @@
from controller.MainController import MainController from controller.MainController import MainController
class HomeController: class HomeController:
""" """
Controller - HomeController Controller - HomeController
desc... desc...
@author Jérémi Nihart / EndMove
@link https://git.endmove.eu/EndMove/WebPicDownloader
@version 1.0.0
@since 2022-08-30
""" """
# Variables # Variables
main_controller = None __main_controller = None
view = None __view = None
# Constructor # Constructor
def __init__(self, controller: MainController) -> None: def __init__(self, controller: MainController) -> None:
self.main_controller = controller self.__main_controller = controller
# START View methods
def set_view(self, view) -> None: def set_view(self, view) -> None:
""" """
[function for view] [function for view]
:view: -> The view that this controller manage. :view: -> The view that this controller manage.
""" """
self.view = view self.__view = view
# END View method
# START View events
def on_change_view(self, frame) -> None:
"""
[event function for view]
:frame: -> The frame we want to launch.
"""
self.__main_controller.change_frame(frame)
def on_download_started(self, url: str, name: str) -> None:
"""
[event function for view]
:url: -> The url of the website to use for pic-download.\n
:name: -> The name of the folder in which put pictures.
"""
pass
# END View events

View File

@ -0,0 +1,41 @@
from controller.MainController import MainController
class InfoController:
"""
Controller - InfoController
desc...
@author Jérémi Nihart / EndMove
@link https://git.endmove.eu/EndMove/WebPicDownloader
@version 1.0.0
@since 2022-08-30
"""
# Variables
__main_controller = None
__view = None
# Constructor
def __init__(self, controller: MainController) -> None:
self.__main_controller = controller
# START View methods
def set_view(self, view) -> None:
"""
[function for view]
:view: -> The view that this controller manage.
"""
self.__view = view
# END View method
# START View events
def on_change_view(self, frame) -> None:
"""
[event function for view]
:frame: -> The frame we want to launch.
"""
self.__main_controller.change_frame(frame)
# END View events

View File

@ -1,10 +1,13 @@
class MainController: class MainController:
""" """
Controller - MainController Controller - MainController
desc... desc...
@author Jérémi Nihart / EndMove
@link https://git.endmove.eu/EndMove/WebPicDownloader
@version 1.0.0
@since 2022-08-30
""" """
# Variables # Variables
__view = None __view = None
@ -13,18 +16,22 @@ class MainController:
def __init__(self) -> None: def __init__(self) -> None:
pass pass
def change_frame(self, frame) -> None: # START View methods
"""
[function for controller]
:frame: -> The frame to be displayed on the screen.
"""
pass
def set_view(self, view) -> None: def set_view(self, view) -> None:
""" """
[function for view] [function for view]
:view: -> The view that this controller manage. :view: -> The view that this controller manage.
""" """
self.__view = view self.__view = view
# END view methods
# START Controller methods
def change_frame(self, frame) -> None:
"""
[function for controller]
:frame: -> The frame we want to display on the window instead of the current frame.
"""
self.__view.show_frame(frame)
# END Controller methods

66
main.py
View File

@ -1,44 +1,38 @@
# from controller.MainController import * from controller.HomeController import HomeController
# from view.MainWindow import * from controller.InfoController import InfoController
# from view.View import View
# from controller.Controller import Controller
# from model.Email import Email
# import tkinter as tk
# class App(tk.Tk): # windows manager
# def __init__(self):
# super().__init__()
# self.title('Tkinter MVC Demo')
# # create a model
# model = Email('hello@pythontutorial.net')
# # create a view and place it on the root window
# view = View(self)
# view.grid(row=0, column=0, padx=10, pady=10)
# # create a controller
# controller = Controller(model, view)
# # set the controller to view
# view.set_controller(controller)
from controller.Frames import Frames
from controller.MainController import MainController from controller.MainController import MainController
from controller.Frames import Frames
from view.HomeView import HomeView
from view.InfoView import InfoView
from view.MainWindow import MainWindow from view.MainWindow import MainWindow
from view.View import View
if __name__ == '__main__': if __name__ == '__main__':
controller = MainController() """
WebPicDownloader is a program developed and maintened by EndMove under Apache 2.0 License. Stealing code is a crime !
Disclamer : The developer of this application can in no way be held responsible if his application is used for illegal purposes.
app = MainWindow(controller) @author Jérémi Nihart / EndMove
@link https://git.endmove.eu/EndMove/WebPicDownloader
@version 1.0.0
@since 2022-08-30
"""
# Create app controllers
main_controller = MainController()
home_controller = HomeController(main_controller)
info_controller = InfoController(main_controller)
app.add_view(Frames.Home, View(app)) # Create app views
app.show_frame(Frames.Home) main_window = MainWindow(main_controller)
home_view = HomeView(main_window, home_controller)
info_controller = InfoView(main_window, info_controller)
app.mainloop() # Add views to main window
main_window.add_view(Frames.Home, home_view)
main_window.add_view(Frames.Info, info_controller)
# Choose the launching view
main_window.show_frame(Frames.Home)
# Start main windows looping (launch program)
main_window.mainloop()

View File

@ -1,8 +1,24 @@
import tkinter as tk import tkinter as tk
from tkinter import ttk from tkinter import ttk
from controller.Frames import Frames
from controller.HomeController import HomeController
class View(ttk.Frame): class HomeView(ttk.Frame):
def __init__(self, parent): """
View - MainWindow
dec...
@author Jérémi Nihart / EndMove
@link https://git.endmove.eu/EndMove/WebPicDownloader
@version 1.0.0
@since 2022-08-30
"""
# Variables
__controller: HomeController = None
# Constructor
def __init__(self, parent, controller: HomeController):
super().__init__(parent) super().__init__(parent)
# create widgets # create widgets
@ -16,62 +32,21 @@ class View(ttk.Frame):
self.email_entry.grid(row=1, column=1, sticky=tk.NSEW) self.email_entry.grid(row=1, column=1, sticky=tk.NSEW)
# save button # save button
self.save_button = ttk.Button(self, text='Save', command=self.save_button_clicked) self.save_button = ttk.Button(self, text='Save', command=self.event_btn)
self.save_button.grid(row=1, column=3, padx=10) self.save_button.grid(row=1, column=3, padx=10)
# message # message
self.message_label = ttk.Label(self, text='', foreground='red') self.message_label = ttk.Label(self, text='coucou toi :D JE SUIS SUPER MAN héhéh', foreground='red')
self.message_label.grid(row=2, column=1, sticky=tk.W) self.message_label.grid(row=2, column=1, sticky=tk.W)
self.grid(row=0, column=0, padx=10, pady=10) # place this frame
# self.grid(row=0, column=0, padx=5, pady=5)
# self.pack(fill='both', expand=True)
# set the controller # Save and setup main controller
self.controller = None self.__controller = controller
controller.set_view(self)
def set_controller(self, controller): def event_btn(self) -> None:
""" print("you clicked ! And now you are on info page ... :D")
Set the controller self.__controller.on_change_view(Frames.Info)
:param controller:
:return:
"""
self.controller = controller
def save_button_clicked(self):
"""
Handle button click event
:return:
"""
if self.controller:
self.controller.save(self.email_var.get())
def show_error(self, message):
"""
Show an error message
:param message:
:return:
"""
self.message_label['text'] = message
self.message_label['foreground'] = 'red'
self.message_label.after(3000, self.hide_message)
self.email_entry['foreground'] = 'red'
def show_success(self, message):
"""
Show a success message
:param message:
:return:
"""
self.message_label['text'] = message
self.message_label['foreground'] = 'green'
self.message_label.after(3000, self.hide_message)
# reset the form
self.email_entry['foreground'] = 'black'
self.email_var.set('')
def hide_message(self):
"""
Hide the message
:return:
"""
self.message_label['text'] = ''

53
view/InfoView.py Normal file
View File

@ -0,0 +1,53 @@
import tkinter as tk
from tkinter import ttk
from controller.Frames import Frames
from controller.InfoController import InfoController
class InfoView(ttk.Frame):
"""
View - MainWindow
dec...
@author Jérémi Nihart / EndMove
@link https://git.endmove.eu/EndMove/WebPicDownloader
@version 1.0.0
@since 2022-08-30
"""
# Variables
__controller: InfoController = None
# Constructor
def __init__(self, parent, controller: InfoController):
super().__init__(parent)
# create widgets
# label
self.label = ttk.Label(self, text='Email:')
self.label.grid(row=1, column=0)
# email entry
# self.email_var = tk.StringVar()
# self.email_entry = ttk.Entry(self, textvariable=self.email_var, width=30)
# self.email_entry.grid(row=1, column=1, sticky=tk.NSEW)
# save button
self.save_button = ttk.Button(self, text='just a button', command=self.event_btn)
self.save_button.grid(row=1, column=3, padx=10)
# message
self.message_label = ttk.Label(self, text='Je suis super man comment allez vous heheheh je suis toutou', foreground='red')
self.message_label.grid(row=2, column=0, sticky=tk.EW)
# place this frame
# self.grid(row=0, column=0, padx=5, pady=5, sticky=tk.NSEW)
# self.pack(fill='both', expand=True)
# Save and setup main controller
self.__controller = controller
controller.set_view(self)
def event_btn(self) -> None:
print("you clicked on the button that is on the info view!")
print("got redirected to the home view :D")
self.__controller.on_change_view(Frames.Home)

View File

@ -1,15 +1,24 @@
from controller.MainController import MainController
import tkinter as tk import tkinter as tk
from tracemalloc import Frame
from controller.MainController import MainController
from controller.Frames import Frames
class MainWindow(tk.Tk): class MainWindow(tk.Tk):
""" """
View - MainWindow View - MainWindow
dec... dec...
@author Jérémi Nihart / EndMove
@link https://git.endmove.eu/EndMove/WebPicDownloader
@version 1.0.0
@since 2022-08-30
""" """
# Variables # Variables
__controller: MainController = None __controller: MainController = None
__views: dict = None __views: dict = None
__current_view = None
# Constructor # Constructor
def __init__(self, controller: MainController): def __init__(self, controller: MainController):
@ -22,11 +31,16 @@ class MainWindow(tk.Tk):
self.title('Tkinter app') self.title('Tkinter app')
self.geometry('450x250') self.geometry('450x250')
self.resizable(False, False) self.resizable(False, False)
self.config(bg='#f7ef38')
self.rowconfigure(0, weight=1)
self.columnconfigure(0, weight=1)
# Save and setup main controller # Save and setup main controller
self.__controller = controller self.__controller = controller
controller.set_view(self) controller.set_view(self)
# START App methods
def add_view(self, frame, view): def add_view(self, frame, view):
""" """
[function for app] [function for app]
@ -34,17 +48,21 @@ class MainWindow(tk.Tk):
:frame: -> the frame id of the view to add. :frame: -> the frame id of the view to add.
""" """
self.__views[frame] = view self.__views[frame] = view
# END App methods
# START Controller methods
def show_frame(self, frame): def show_frame(self, frame):
""" """
[function for app & controller] [function for app & controller]
:frame: -> the frame if of the view to display. :frame: -> the frame if of the view to display.
""" """
print(self.__views) new_view = self.__views.get(frame)
if self.__views.get(frame): if new_view:
self.__views.get(frame).tkraise() if self.__current_view:
#self.view = self.__views.get(frame) self.__current_view.pack_forget()
#self.view.grid(row=0, column=0) new_view.pack(fill=tk.BOTH, expand=False)
self.__current_view = new_view
else: else:
raise ValueError("Unable to find the requested Frame") raise ValueError("Unable to find the requested Frame")
# END Controller methods