Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Class architecture refactoring #22

Merged
merged 25 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
9f6809e
[001_class_architecture_refactoring], issue #1, Removed BaseWindow cl…
dmanzione Jul 8, 2024
c534a2c
[001_class_architecture_refactoring], issue #1, Moved EntryVariable a…
dmanzione Jul 8, 2024
0206afb
[001_class_architecture_refactoring], issue #1, Restructured the clas…
dmanzione Jul 12, 2024
b9cc1e3
[001_class_architecture_refactoring], issue #1, Replaced the 'DatGene…
dmanzione Jul 13, 2024
2bd60ed
[001_class_architecture_refactoring], issue #1, Replaced diagram data…
dmanzione Jul 15, 2024
63ccd8c
[001_class_architecture_refactoring], issue #1, Changed 'PlotSettings…
dmanzione Jul 15, 2024
4f09de9
[001_class_architecture_refactoring], issue #1, Replaced 'GuiPlotFiel…
dmanzione Jul 15, 2024
c70f085
[001_class_architecture_refactoring], issue #1, Cleaned code in 'gui_…
dmanzione Jul 15, 2024
44c18de
[001_class_architecture_refactoring], issue #1, Created 'support.py' …
dmanzione Jul 20, 2024
fe630e9
[001_class_architecture_refactoring], issue #1, Labelled image handling
dmanzione Jul 20, 2024
150b897
[001_class_architecture_refactoring], issue #1, .pli handling
dmanzione Jul 20, 2024
0971086
[001_class_architecture_refactoring], issue #1, 'TabContentBuilder' c…
dmanzione Jul 20, 2024
9c2770f
[001_class_architecture_refactoring], issue #1, kwargs check
dmanzione Jul 20, 2024
2cad5de
[001_class_architecture_refactoring], issue #1, Added 'support.py' mo…
dmanzione Jul 20, 2024
f0ca192
[001_class_architecture_refactoring], issue #1, 'TuInp' building
dmanzione Jul 20, 2024
ef94851
[001_class_architecture_refactoring], issue #1, 'WidgetTooltip' inher…
dmanzione Jul 20, 2024
efc54ba
Merge branch 'master' into 001_class_architecture_refactoring
lelaus Jul 21, 2024
deb2d13
[001_class_architecture_refactoring], issue #1, Task 4 review changes
dmanzione Jul 22, 2024
7ebee38
[001_class_architecture_refactoring], issue #1, Task 4 review changes
dmanzione Jul 22, 2024
0da17fe
[001_class_architecture_refactoring], issue #1, Task 5 review changes
dmanzione Jul 22, 2024
4731b99
[001_class_architecture_refactoring], issue #1, Task 7 review changes
dmanzione Jul 22, 2024
233fdf9
[001_class_architecture_refactoring], issue #1, Review changes
dmanzione Jul 22, 2024
1e280bf
[001_class_architecture_refactoring], issue #1, Task 10 review changes
dmanzione Jul 22, 2024
8d3165b
[001_class_architecture_refactoring], issue #1, Task 12 review changes
dmanzione Jul 22, 2024
07b9f2e
[001_class_architecture_refactoring], issue #1, Task 7 review changes
dmanzione Jul 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
400 changes: 201 additions & 199 deletions tugui/gui_configuration.py

Large diffs are not rendered by default.

131 changes: 115 additions & 16 deletions tugui/gui_widgets.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,103 @@
import os
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
from PIL import Image, ImageTk


class EntryVariable:
"""
Class defining a variable having a corresponding Entry object. Its value
is validated when set.
"""
def __init__(self, frame: tk.Frame, width: int, col: int, row: int, end: str) -> None:
"""
Constructor requiring the Frame object onto which putting the Entry.
The Entry width, as well as the column and row indices are passed to
configure the Entry object within the frame.
"""
# Instantiate the string variable
self.var = tk.StringVar()
# Instantiate the entry field
self.entry = ttk.Entry(frame, width = width, textvariable=self.var)
# Place the entry in the frame grid
self.entry.grid(column = col, row = row, sticky = 'ew')

# Register the validation funtion of the Entry widget
valid_entry = (self.entry.register(self.validate), '%P')
# Configure the entry for checking the validity of its content when the
# widget looses focus
self.entry.configure(validate='focusout', validatecommand=valid_entry)

# Entry file extension
self.entry_extension = end

def validate(self, event=None, newval: str = ""):
"""
Method that checks if the entry is valid. The "end" parameter indicates the
extension to check against.
"""
# Check the entry value against the allowed extension
if re.match(r"^.*\." + self.entry_extension + "$", newval) is not None:
# The entry is valid if a match is found
print("The entry is valid!")
self.entry.configure(foreground="#343638")
return True
else:
# If no match is found, handle the invalid case only if the entry value is not empty
if newval != "":
self.on_invalid()
return False

def on_invalid(self):
"""
Show the error message if the data is not valid.
"""
error_message = "The entry is not valid: please provide a path to a file with the valid \"" + self.entry_extension + "\" extension!"
print(error_message)
# Highlight the entry color in red
self.entry.configure(foreground="red")
# Show the error message as a pop-up window
messagebox.showerror("Error", error_message)


class StatusBar(ttk.Frame):
"""
Class describing a Frame where a label is shown. This represents a status bar
that provides useful log to the user.
"""
def __init__(self, container, color: str = 'light gray'):
# Initialize the Style object
s = ttk.Style()
# Configure the style for the status bar frame
s.configure('self.TFrame', background=color, border=1, borderwidth=1, relief=tk.GROOVE)
# Configure the style for the status bar label
s.configure('self.TLabel', background=color)

# Call the superclass initializer passing the built style
super().__init__(container, style='self.TFrame')

# Declare an empty label providing the status messages
self.label = ttk.Label(self, text="", style='self.TLabel')
# Align the label on the left
self.label.pack(side=tk.LEFT, padx=3, pady=3)

# Configure the status bar in order to fill all the space in the horizontal direction
self.grid(sticky='ew')

def set_text(self, new_text):
"""
Method that allow the modification of the status bar text.
"""
self.label.configure(text=new_text)

def clear_label(self):
"""
Method that clears any text already present in the label status bar.
"""
self.set_text("")


class CustomNotebook(ttk.Notebook):
"""
Class that provides a customization of the ttk.Notebook class that enables
Expand Down Expand Up @@ -165,24 +259,29 @@ def set_text(self, new_text: str):
self._btn.configure(text=new_text)


class LabelImage(ttk.Label):
def provide_label_image(container, img_path: str) -> ttk.Label:
"""
Class that provides a label filled with an image.
Method that provides a label filled with an image. It builds an instance
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, change "Method" into "Function" since outside a class.

of the 'ttk.Label' class by receiving the contaniner to put the label
into, as well as the path to the image file to load and assign to the
label.
The loaded image is added as an attribute to the 'ttk.Label' instance so
to keep a reference to it and prevent it to be garbage collected, thus
allowing to be correctly shown.
"""
def __init__(self, container, img_path: str):
"""
Construct an instance of the 'LabelImage' class by receiving the
contaniner to put the label into, as well as the path to the
image file to load and assign to the label.
"""
# Call the superclass constructor by passing the container
super().__init__(container)
# Load the image
self.label_image = Image.open(img_path)
self.label_image = ImageTk.PhotoImage(self.label_image)
# Instantiate the 'ttk.Label' class
label = ttk.Label(container)
# Load the image
label_image = Image.open(img_path)
label_image = ImageTk.PhotoImage(label_image)
# Configure the label by assigning the image to it
label.configure(image=label_image)
# Add an attribute to the label to keep a reference to the image and prevent it
# from being garbage collected
label.image = label_image

# Configure the label by assigning the image to it
self.configure(image=self.label_image)
# Return the label
return label


class OnOffClickableLabel(ttk.Frame):
Expand Down Expand Up @@ -275,7 +374,7 @@ def deactivate_label(self):
self._lbl.unbind('<Button-1>', self.click_event)


class WidgetTooltip(object):
class WidgetTooltip():
"""
Class that provides a tooltip for a given widget passed as argument to
this class constructor.
Expand Down
Loading
Loading