Skip to content

Commit

Permalink
Improve code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
Nitro4542 committed Sep 6, 2024
1 parent b740776 commit 5227845
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 18 deletions.
7 changes: 5 additions & 2 deletions src/desktop-groups/__main__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from . import gui
"""
Main part of desktop-groups module
"""
import argparse
from . import gui


if __name__ == "__main__":
Expand All @@ -10,4 +13,4 @@

# Open GUI
app = gui.App(args.filename)
app.mainloop()
app.mainloop()
17 changes: 12 additions & 5 deletions src/desktop-groups/group.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
"""
Desktop group module for reading JSON files and storing their values
"""
import importlib.resources
import json
import sys
Expand All @@ -6,9 +9,11 @@


class Group:
"""
A desktop group
"""
def __init__(self, name: str, icon: str = None):
"""
A desktop group
:param name: Group name
:param icon: Group icon
"""
Expand Down Expand Up @@ -39,17 +44,19 @@ def remove_item(self, name: str):


class DGFileGroup(Group):
"""
A desktop group created from a file
"""
def __init__(self, dg_file: str):
"""
Desktop group from a file
:param dg_file: Location of file
"""
# Read .desktopgroup file
with open(dg_file, 'r') as file:
with open(dg_file, 'r', encoding="utf-8") as file:
self.data = json.load(file)

# Read JSON schema
with importlib.resources.open_text('desktop-groups', 'desktopgroups.schema.json') as schema:
with importlib.resources.open_text('desktop-groups', 'desktopgroups.schema.json', encoding="utf-8") as schema:
self.schema = json.load(schema)

# Validate JSON
Expand All @@ -74,7 +81,7 @@ def validate(self):
"""
try:
jsonschema.validate(self.data, self.schema)
except jsonschema.exceptions as e:
except Exception as e:
print(f'Error: {e}')
return False
return True
38 changes: 27 additions & 11 deletions src/desktop-groups/gui.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
"""
Desktop group GUI
"""
import subprocess
import sys
from tkinter import IntVar, font

from PIL import Image
from . import group
import customtkinter
from . import group


class GroupItemsScrollableFrame(customtkinter.CTkScrollableFrame):
"""
Displays all the items in the items list
"""
def __init__(self, master, items: list, scale: float, base_font: customtkinter.CTkFont, **kwargs):
"""
Displays all the items in the items list
:param master: Root frame
:param items: List containing all items
"""
Expand All @@ -28,10 +33,12 @@ def __init__(self, master, items: list, scale: float, base_font: customtkinter.C
# Add buttons to frame
for index, list_item in enumerate(self.item_list):
if list_item.get('icon'):
img = customtkinter.CTkImage(light_image=Image.open(list_item.get('icon')), size=((int(24 * scale)), int((24 * scale))))
img = customtkinter.CTkImage(light_image=Image.open(list_item.get('icon')),
size=((int(24 * scale)), int((24 * scale))))
icon = customtkinter.CTkLabel(self, text="", image=img)
icon.grid(row=index, column=0, padx=10, pady=10)
radio_button = customtkinter.CTkRadioButton(self, text=list_item.get('name'), font=base_font, variable=self.radio_var, value=index)
radio_button = customtkinter.CTkRadioButton(self, text=list_item.get('name'), font=base_font,
variable=self.radio_var, value=index)
radio_button.grid(row=index, column=1, padx=10, pady=10, sticky="ew")

def get_command(self):
Expand All @@ -40,9 +47,11 @@ def get_command(self):


class GroupInfoFrame(customtkinter.CTkFrame):
"""
Displays logo and text of a group
"""
def __init__(self, master, icon: str, text: str, scale: float, title_font: customtkinter.CTkFont, **kwargs):
"""
Displays logo and text of a group
:param master: Root frame
:param icon: Path to icon
:param text: Name of group
Expand All @@ -68,9 +77,11 @@ def __init__(self, master, icon: str, text: str, scale: float, title_font: custo
self.gi_icon.grid(row=0, column=0, padx=10, pady=10, sticky="w")

class ButtonFrame(customtkinter.CTkFrame):
"""
Displays the 'Continue' and 'Cancel' button
"""
def __init__(self, master, group_items_frame: GroupItemsScrollableFrame, base_font: customtkinter.CTkFont, **kwargs):
"""
Displays the 'Continue' and 'Cancel' button
:param master: Root frame
"""
super().__init__(master, **kwargs)
Expand All @@ -84,10 +95,12 @@ def __init__(self, master, group_items_frame: GroupItemsScrollableFrame, base_fo
self.columnconfigure(1, weight=1)

# Configure and place buttons
self.continue_button = customtkinter.CTkButton(self, text="Continue", font=base_font, command=lambda: self.continue_button_command())
self.continue_button = customtkinter.CTkButton(self, text="Continue", font=base_font,
command=lambda: self.continue_button_command())
self.continue_button.grid(row=0, column=0, padx=10, pady=10, sticky="ew")

self.cancel_button = customtkinter.CTkButton(self, text="Cancel", font=base_font, command=lambda: self.cancel_button_command())
self.cancel_button = customtkinter.CTkButton(self, text="Cancel", font=base_font,
command=lambda: self.cancel_button_command())
self.cancel_button.grid(row=0, column=1, padx=10, pady=10, sticky="ew")

def cancel_button_command(self):
Expand All @@ -101,9 +114,11 @@ def continue_button_command(self):


class App(customtkinter.CTk, group.DGFileGroup):
"""
Main window for opening a .desktopgroup file
"""
def __init__(self, dg_file: str):
"""
Main window for opening a .desktopgroup file
:param dg_file: .desktopgroup file
"""
# Initialize CustomTkinter
Expand Down Expand Up @@ -178,7 +193,8 @@ def _set_geometry(self):
def _calculate_window_position(self):
"""Calculates window position"""
win_x = int(((self.screen_width / 2) - (self.window_width / 2)) * self.scale_factor)
win_y = int(((self.screen_height / 2) - (self.window_height / 1.5)) * self.scale_factor) + (80 * self.scale_factor)
win_y = (int(((self.screen_height / 2) - (self.window_height / 1.5)) * self.scale_factor) +
(80 * self.scale_factor))
return win_x, win_y

def _window_geometry(self):
Expand All @@ -200,4 +216,4 @@ def _create_font_objects(self):

# Create objects
self.base_font = customtkinter.CTkFont(default_font.cget('family'))
self.title_font = customtkinter.CTkFont(default_font.cget('family'), 22, "bold")
self.title_font = customtkinter.CTkFont(default_font.cget('family'), 22, "bold")

0 comments on commit 5227845

Please sign in to comment.