diff --git a/App.py b/App.py index 4318308..3d12843 100644 --- a/App.py +++ b/App.py @@ -45,7 +45,6 @@ class App: def __init__(self, root, to_bypass, debug): # Toplevel windows # The names are PascalCase to be identical to __class__.__name__ of each Toplevel class. - self.topleveldonate = None self.toplevelabouttpp = None self.toplevelsocial = None @@ -57,6 +56,7 @@ def __init__(self, root, to_bypass, debug): self.autosave_db = True self.loading_tk = None self.dir_path = self.find_current_dir_path() + self.dir_path_of_main = self.find_the_path_of_main() self.transparency = None self.help_menu = None self.database_menu = None @@ -88,7 +88,9 @@ def __init__(self, root, to_bypass, debug): self.searchbox = None self.search_button = None self.searchtoplevel = None - self.term = None # The keyword that the user provided + self.search_photo = None + self.search_labelframe = None + self.seach_keyword = None # The keyword that the user provided self.search_counter = 1 self.create_search_ui() self.top_parent_label = ttk.Label(self.root) @@ -303,7 +305,8 @@ def create_search_ui(self): width=10, border=0) self.searchbox.pack(side="left") self.searchbox.bind("", self.search_handler) - self.search_photo = Image.open(os.path.join(self.dir_path, "source\\multimedia\\images\\misc\\search.png")) + self.search_photo = Image.open(os.path.join(self.dir_path_of_main, + "source\\multimedia\\images\\misc\\search.png")) self.search_photo = self.search_photo.resize((35, 35), Image.ANTIALIAS) self.search_photo = ImageTk.PhotoImage(image=self.search_photo, master=self.search_label) self.search_button = tk.Button(self.search_label, image=self.search_photo, @@ -320,8 +323,8 @@ def search_site(self): """Picks the user's input from the search box, searches the TPP site and retrieves the results from the first page""" self.searchbox.focus_set() - self.term = self.search_text_var.get().strip() - results = SearchTerm(term=self.term, page_number=1, debug=False) + self.seach_keyword = self.search_text_var.get().strip() + results = SearchTerm(term=self.seach_keyword, page_number=1, debug=False) print(results.list) self.search_text_var.set("") self.searchtoplevel = ToplevelSearch(root=self.root, controller=self, results=results.list) @@ -329,7 +332,7 @@ def search_site(self): def search_site_load_more(self): """Loads more results for the given keyword. The function is called from ToplevelSearch class""" self.search_counter += 1 - results = SearchTerm(term=self.term, page_number=self.search_counter, debug=False) + results = SearchTerm(term=self.seach_keyword, page_number=self.search_counter, debug=False) # Merge the new results to the old ones and refill the treeview self.searchtoplevel.fetched_news += results.list self.searchtoplevel.fill_treeview() @@ -465,6 +468,31 @@ def find_current_dir_path(self) -> str | os.PathLike: print(f'Script: {self.dir_path}') return self.dir_path + def find_the_path_of_main(self) -> str: + """ + Finds and returns the path of the main.py or the temporary folder (MEIPP) if the program runs as an exe. + :return: The folder path + """ + if getattr(sys, 'frozen', False): + print(getattr(sys, 'frozen', False)) + # The temporary path of the file when the app runs as an .exe + self.dir_path_of_main = os.path.dirname(os.path.realpath(__file__)) + # If the path until this step contains \\scrape_tpp_gui, get the parent dir, which is a temp dir(MEIPP). + # self.dir_path = os.path.dirname(self.dir_path) + print(f"{self.name_of_class}>Exe (dir_path_of_main):", self.dir_path_of_main) + return self.dir_path_of_main + elif __file__: + self.dir_path = os.path.dirname(__file__) # We need the parent of the parent of this directory + print(f'{self.name_of_class}>Script (self.dir_path): {self.dir_path}') + return self.dir_path + + @property + def name_of_class(self): + """ + :return: The name of the class in lower case. + """ + return self.__class__.__name__.lower() + ####################### # Main Menu functions # ####################### diff --git a/source/classes/search.py b/source/classes/search.py index 1cdfec1..19104c9 100644 --- a/source/classes/search.py +++ b/source/classes/search.py @@ -1,7 +1,7 @@ import requests from bs4 import BeautifulSoup from scrape_tpp_gui.trace_error import trace_error -from NewsDataclass import NewsDataclass +from scrape_tpp_gui.source.classes.NewsDataclass import NewsDataclass class SearchTerm: @@ -58,9 +58,6 @@ def scrape_data(self): for _date in item.find("div", class_="entry-meta"): date = _date.text.strip() # print(_date.text) - #if len(date) == 0: - - # The date = "" will raise an IndexError in Newsdataclass, but we don't care about the unixtimestamp # in this occasion. Thus, debug is set to False. It remains True, for the rest of the program which uses # the Newsdataclass diff --git a/source/classes/searchtoplevel.py b/source/classes/searchtoplevel.py index f1d7760..b75571a 100644 --- a/source/classes/searchtoplevel.py +++ b/source/classes/searchtoplevel.py @@ -2,12 +2,11 @@ import tkinter as tk from tkinter import ttk import tkinter.font as font - from scrape_tpp_gui.source.classes.database.SubPageReaderDB import SubPageReader from scrape_tpp_gui.source.classes.database.helper import ToplevelArticleDatabase from scrape_tpp_gui.source.classes.generictoplevel import GenericToplevel from scrape_tpp_gui.trace_error import trace_error -from NewsDataclass import NewsDataclass +from scrape_tpp_gui.source.classes.NewsDataclass import NewsDataclass from scrape_tpp_gui.helper_functions import sortby, center, headers @@ -17,6 +16,7 @@ class ToplevelSearch(GenericToplevel): def __init__(self, controller, root: tk.Tk, results: list[NewsDataclass]): super().__init__(controller=controller, root=root) + self.toplevel.withdraw() self.toplevel: tk.Toplevel = self.toplevel self.controller = controller self.root = root @@ -30,6 +30,8 @@ def __init__(self, controller, root: tk.Tk, results: list[NewsDataclass]): self.font = font.Font(size=14) self.big_frame = None self.tree = None + self.bar_menu = None + self.main_menu = None self.right_click_menu = None self.create_menu() self.create_ui() @@ -37,6 +39,7 @@ def __init__(self, controller, root: tk.Tk, results: list[NewsDataclass]): self.fill_treeview() self.create_binds() center(self.toplevel, self.root) + self.toplevel.deiconify() self.toplevel.lift() def create_menu(self): @@ -48,10 +51,13 @@ def create_menu(self): self.main_menu = tk.Menu(self.bar_menu, font='Arial 10', tearoff=0, background='black', fg='white') self.bar_menu.add_cascade(label='Menu', menu=self.main_menu, background='black') self.main_menu.add_command(label='Load more results', command=self.controller.search_site_load_more) + # self.main_menu.add_command(label='root', command=lambda: center(self.toplevel, self.root)) + # self.main_menu.add_command(label='screen', command=lambda: center(self.toplevel)) # Right click menu only for the treeview self.right_click_menu = tk.Menu(font='Arial 10', tearoff=0) # Lambda here is needed because there is no event to be passed. If no lambda is used, an error will be raised self.right_click_menu.add_command(label='Show article', command=lambda: self.show_main_article(event=None)) + self.right_click_menu.add_command(label='Load more results', command=self.controller.search_site_load_more) def create_ui(self): """Constructs the user interface""" @@ -108,9 +114,12 @@ def fill_treeview(self): # Fix the lengths self.tree.column(column='Title', minwidth=100, width=int(max(titles_length)), stretch=True) self.tree.column(column='Date', minwidth=150, width=int(max(dates_length)), stretch=False) - # Adjust the x axis after the scraped data is loaded in the treeview - self.x = int(max(titles_length)) + int(max(dates_length)) + 150 + # Adjust the x-axis after the scraped data is loaded in the treeview + self.x = int(max(titles_length)) + int(max(dates_length)) + self.toplevel.withdraw() self.toplevel.geometry(f"{self.x}x{self.y}") + center(self.toplevel) + self.toplevel.deiconify() print(f"ToplevelSearch>fill_treeview()>news inserted") def create_binds(self): @@ -186,6 +195,7 @@ def toplevel_quit(self): from scrape_tpp_gui.helper_functions import center, tkinter_theme_calling, parse_arguments from scrape_tpp_gui.trace_error import trace_error from search import SearchTerm + center(root) tkinter_theme_calling(root) results = SearchTerm(term="κουλης", page_number=1, debug=False) diff --git a/source/version/version.json b/source/version/version.json index d7b2aea..8b44013 100644 --- a/source/version/version.json +++ b/source/version/version.json @@ -1,3 +1,3 @@ { - "version": "21-12-2022" + "version": "07-03-2023" }