コード例 #1
0
ファイル: guiApp.py プロジェクト: MuskoM/KCK-proj
class NotebookTab:
    def __init__(self, notebook, **kwargs):
        config_file = open('./bin/config.conf')
        self.URL = config_file.readline().split("=")[1]
        config_file.close()
        self.title = kwargs["text"]
        self.files_path = kwargs["path"]
        self.download_function = kwargs["download_function"]
        self.articles = []
        self.titles = []
        self.page_no = 0
        self.news_module = News()
        self.notebook = notebook
        self.gov_articles_tab = ttk.Frame(notebook)
        notebook.add(self.gov_articles_tab, text=kwargs["text"])

        # Article label
        self.news_title_label = ttk.Label(
            self.gov_articles_tab,
            text="Title",
            font=Font(size=H1_FONT_SIZE),
        )
        self.news_title_label.grid(sticky="NSWE")
        # Container for the article
        self.news_reader_fragment = ScrolledText(
            self.gov_articles_tab, font=Font(size=PARAGRAPGH_FONT_SIZE))
        self.news_reader_fragment.insert(
            '1.0', 'Download and Load Articles to see anything')
        self.news_reader_fragment['state'] = 'disabled'
        self.news_reader_fragment.grid(sticky="NSWE")
        # Article number indicator
        self.news_article_page_progress = Progressbar(self.gov_articles_tab,
                                                      mode="determinate")
        self.news_article_page_progress['value'] = 0
        self.news_article_page_progress.grid(sticky="NSWE")
        self.news_article_page_progress.update()
        self.news_title_label.config(
            wraplength=self.news_article_page_progress.winfo_width())
        buttons_grid_frame = ttk.Frame(self.gov_articles_tab)
        buttons_grid_frame.grid(row=3, column=0)
        # Buttons for prev and next article
        article_buttons_frame = ttk.Frame(buttons_grid_frame, relief="flat")
        article_buttons_frame.grid(row=0)
        previous_article_button = ttk.Button(article_buttons_frame,
                                             command=self.get_previous_article,
                                             text="Previous article").grid(
                                                 row=0, column=0)
        next_article_button = ttk.Button(article_buttons_frame,
                                         text="Next article",
                                         command=self.get_next_article).grid(
                                             row=0, column=1)

        # Frame for get_article_type buttons
        get_health_articles_button = ttk.Button(
            article_buttons_frame,
            text="Load Articles",
            command=self.get_articles).grid(row=0, column=2)

        # Frame for download_article_type buttons
        download_health_articles_button = ttk.Button(
            article_buttons_frame,
            text="Download Articles",
            command=self.download_articles).grid(row=0, column=3)

        self.gov_articles_tab.columnconfigure(self.news_title_label, weight=1)
        self.gov_articles_tab.rowconfigure(self.news_title_label, weight=1)
        self.gov_articles_tab.rowconfigure(self.news_reader_fragment,
                                           weight=10)
        self.gov_articles_tab.columnconfigure(self.news_reader_fragment,
                                              weight=10)

    def get_previous_article(self):
        try:
            self.page_no -= 1
            self.news_article_page_progress["value"] = self.page_no
            self.news_reader_fragment["state"] = "normal"
            self.news_reader_fragment.delete("1.0", "end")
            self.news_reader_fragment.insert("1.0",
                                             self.articles[self.page_no])
            self.news_reader_fragment["state"] = "disabled"
            self.news_title_label["text"] = self.titles[
                self.news_article_page_progress["value"]]
        except IndexError:
            self.page_no += 1

    def get_next_article(self):
        try:
            self.page_no += 1
            self.news_article_page_progress["value"] = self.page_no
            self.news_reader_fragment["state"] = "normal"
            self.news_reader_fragment.delete("1.0", "end")
            self.news_reader_fragment.insert("1.0",
                                             self.articles[self.page_no])
            self.news_reader_fragment["state"] = "disabled"
            self.news_title_label["text"] = self.titles[
                self.news_article_page_progress["value"]]
        except IndexError:
            self.page_no -= 1

    def get_articles(self):
        download = askyesno('Confirm?',
                            f"Do you want to view {self.title}",
                            parent=self.notebook)
        if download:
            self.articles.clear()
            self.titles.clear()
            self.news_article_page_progress["value"] = 0
            for file in os.listdir(self.files_path):
                article_file = open(self.files_path + file, encoding='utf-8')
                self.titles.append(article_file.readline())
                self.articles.append(article_file.read())
            self.news_reader_fragment["state"] = "normal"
            self.news_reader_fragment.delete("1.0", "end")
            self.news_reader_fragment.insert("1.0", self.articles[0])
            self.news_reader_fragment["state"] = "disabled"
            self.news_title_label["text"] = self.titles[0]
            self.news_article_page_progress["maximum"] = len(self.articles)

    def get_articles_wthout_confirmation(self):
        self.articles.clear()
        self.titles.clear()
        self.news_article_page_progress["value"] = 0
        for file in os.listdir(self.files_path):
            article_file = open(self.files_path + file, encoding='utf-8')
            self.titles.append(article_file.readline())
            self.articles.append(article_file.read())
        self.news_reader_fragment["state"] = "normal"
        self.news_reader_fragment.delete("1.0", "end")
        self.news_reader_fragment.insert("1.0", self.articles[0])
        self.news_reader_fragment["state"] = "disabled"
        self.news_title_label["text"] = self.titles[0]
        self.news_article_page_progress["maximum"] = len(self.articles)

    def download_articles(self):
        download = askyesno(
            "Download?",
            f"Do you want to download {self.title} ?, This might take a while.",
            parent=self.notebook)
        if download:
            if self.download_function is not None:
                self.download_function
            else:
                if self.URL is None:
                    self.articles_popup()
                else:
                    try:
                        self.news_module.get_my_articles(self.URL)
                    except Exception:
                        showerror("ERROR!",
                                  "Bad URL, try again!",
                                  parent=self.notebook)
                        self.articles_popup()

    def articles_popup(self):
        self.dialog = tk.Toplevel(self.notebook)
        self.dialog.title("Input URL")
        self.county_input_label = ttk.Label(self.dialog,
                                            text="Input URL here:",
                                            padding="0px 0px 0px 5px")
        self.county_input_label.grid(sticky="NSWE")

        self.URL_input = ttk.Entry(self.dialog)
        self.URL_input.grid(sticky="NSWE")

        self.confirm_dialog_btn = ttk.Button(self.dialog,
                                             text="Confirm",
                                             command=self.on_click)
        self.confirm_dialog_btn.grid(sticky="NSEW")

    def on_click(self):
        self.URL = self.URL_input.get()
        try:
            self.news_module.get_my_articles(self.URL)
        except Exception:
            showerror("ERROR!", "Bad URL, try again!", parent=self.dialog)
            self.articles_popup()
        config_file = open('./bin/config.conf', "w")
        config_file.write(f"URL={self.URL}")
        config_file.close()
        self.dialog.destroy()