Begin refactor
This commit is contained in:
117
src/GUI.py
Normal file
117
src/GUI.py
Normal file
@@ -0,0 +1,117 @@
|
||||
import tkinter as tk
|
||||
from tkinter import filedialog
|
||||
from PIL import ImageTk
|
||||
from ImageContainer import ImageContainer
|
||||
|
||||
|
||||
class GUI:
|
||||
"""The GUI class responsible for the main application GUI.
|
||||
|
||||
@warning This class is a singleton.
|
||||
"""
|
||||
_instance = None
|
||||
|
||||
# If the GUI has been initialised.
|
||||
_isInitialised = False
|
||||
|
||||
_currentImage = None
|
||||
|
||||
def __new__(cls):
|
||||
if cls._instance is None:
|
||||
cls._instance = super(GUI, cls).__new__(cls)
|
||||
return cls._instance
|
||||
|
||||
def initialise(self):
|
||||
"""Initialise the GUI."""
|
||||
|
||||
if self._isInitialised:
|
||||
return
|
||||
else:
|
||||
self._isInitialised = True
|
||||
|
||||
# Main window
|
||||
root = tk.Tk()
|
||||
root.title("Image Viewer")
|
||||
root.geometry("800x600")
|
||||
root.config(bg="white")
|
||||
icon = tk.PhotoImage(file='icon.png')
|
||||
root.tk.call('wm', 'iconphoto', root._w, icon)
|
||||
root.minsize(800, 600)
|
||||
|
||||
# Menus
|
||||
menu = tk.Menu(root)
|
||||
root.config(menu=menu)
|
||||
file_menu = tk.Menu(menu, tearoff=0)
|
||||
menu.add_cascade(label="File", menu=file_menu)
|
||||
file_menu.add_command(label="Open Image", command=lambda: self._openImage())
|
||||
file_menu.add_command(label="Save Image", command=lambda: _save_image(OPENED_IMAGE))
|
||||
file_menu.add_command(label="Exit", command=root.quit)
|
||||
|
||||
test_menu = tk.Menu(menu, tearoff=0)
|
||||
menu.add_cascade(label="Test", menu=test_menu)
|
||||
test_menu.add_command(label="Padding", command=lambda: padding(OPENED_IMAGE,
|
||||
border_width=50) if OPENED_IMAGE is not None else print(
|
||||
"No image opened."))
|
||||
test_menu.add_command(label="Crop", command=lambda: crop(OPENED_IMAGE) if OPENED_IMAGE is not None else print(
|
||||
"No image opened."))
|
||||
test_menu.add_command(label="Resize",
|
||||
command=lambda: resize(OPENED_IMAGE) if OPENED_IMAGE is not None else print(
|
||||
"No image opened."))
|
||||
test_menu.add_command(label="Copy", command=lambda: copy(OPENED_IMAGE) if OPENED_IMAGE is not None else print(
|
||||
"No image opened."))
|
||||
test_menu.add_command(label="Greyscale",
|
||||
command=lambda: grayscale(OPENED_IMAGE) if OPENED_IMAGE is not None else print(
|
||||
"No image opened."))
|
||||
test_menu.add_command(label="Hsv", command=lambda: hsv(OPENED_IMAGE) if OPENED_IMAGE is not None else print(
|
||||
"No image opened."))
|
||||
test_menu.add_command(label="Hue Shifted",
|
||||
command=lambda: hue_shifted(OPENED_IMAGE, hue=50) if OPENED_IMAGE is not None else print(
|
||||
"No image opened."))
|
||||
test_menu.add_command(label="Smoothed",
|
||||
command=lambda: smoothing(OPENED_IMAGE) if OPENED_IMAGE is not None else print(
|
||||
"No image opened."))
|
||||
test_menu.add_command(label="Rotated", command=lambda: rotation(OPENED_IMAGE,
|
||||
rotation_angle=90) if OPENED_IMAGE is not None else print(
|
||||
"No image opened."))
|
||||
|
||||
clipboard_menu = tk.Menu(menu, tearoff=0)
|
||||
menu.add_cascade(label="Clipboard", menu=clipboard_menu)
|
||||
|
||||
image_menu = tk.Menu(menu, tearoff=0)
|
||||
menu.add_cascade(label="Image", menu=image_menu)
|
||||
|
||||
tools_menu = tk.Menu(menu, tearoff=0)
|
||||
menu.add_cascade(label="Tools", menu=tools_menu)
|
||||
|
||||
shapes_menu = tk.Menu(menu, tearoff=0)
|
||||
menu.add_cascade(label="Shapes", menu=shapes_menu)
|
||||
|
||||
colors_menu = tk.Menu(menu, tearoff=0)
|
||||
menu.add_cascade(label="Colors", menu=colors_menu)
|
||||
|
||||
layer_menu = tk.Menu(menu, tearoff=0)
|
||||
menu.add_cascade(label="Layer", menu=layer_menu)
|
||||
|
||||
filter_menu = tk.Menu(menu, tearoff=0)
|
||||
menu.add_cascade(label="Filter", menu=filter_menu)
|
||||
#
|
||||
|
||||
# Frame to hold image
|
||||
imgframe = tk.Frame(root, width=500, height=500, bg="lightgray", relief="sunken", bd=2)
|
||||
imgframe.pack(side="top", pady=10)
|
||||
|
||||
# Label to display image
|
||||
image_label = tk.Label(imgframe, width=500, height=500, bg="white")
|
||||
image_label.pack(expand=True)
|
||||
|
||||
root.mainloop()
|
||||
|
||||
def _openImage(self) -> None:
|
||||
file_path = filedialog.askopenfilename(
|
||||
filetypes=[("Image files", "*.jpg *.jpeg *.png *.gif *.bmp")]
|
||||
)
|
||||
if not file_path:
|
||||
return
|
||||
|
||||
self._currentImage = ImageContainer()
|
||||
self._currentImage.loadImage(file_path)
|
||||
Reference in New Issue
Block a user