_tkinter.TclError displays on some news articles

134 views Asked by At

Currently, I am writing a program that allows the user to input a link from a news site and then my program will display the title, author, and the summary of the inputted news article. I am currently using the newspaper module.

However, I realized that for some news articles, I get a _tkinter.TclError when newspaper tries to summarize the article. However, the title and author works. I suspect it is a tkinter problem, specifically in code self.sumText.insert('1.0',text).

Here is my code

import tkinter as tk
import tkinter.ttk as ttk
from textblob import TextBlob
from newspaper import Article
import validators

class SummarizerguiApp:
    def __init__(self, master=None):
        # build ui
        self.root = tk.Tk() if master is None else tk.Toplevel(master)
        self.root.title('Summarizer')
        self.title = ttk.Label(self.root)
        self.title.configure(text="Title")
        self.title.place(anchor="nw", x=296, y=10)
        self.titleText = tk.Text(self.root,wrap=tk.WORD)
        self.titleText.configure(background="#cacaca", height=3, width=75)
        self.titleText.place(anchor="nw", x=15, y=44)
        self.author = ttk.Label(self.root)
        self.author.configure(text="Author")
        self.author.place(anchor="nw", x=280, y=100)
        self.authorText = tk.Text(self.root)
        self.authorText.configure(background="#c9c9c9", height=1, width=75)
        self.authorText.place(anchor="nw", x=15, y=134)
        self.date = ttk.Label(self.root)
        self.date.configure(text="Publishing Date")
        self.date.place(anchor="nw", x=256, y=190)
        self.dateText = tk.Text(self.root)
        self.dateText.configure(background="#c9c9c9", height=1, width=75)
        self.dateText.place(anchor="nw", x=15, y=224)
        self.summary = ttk.Label(self.root)
        self.summary.configure(text="Author")
        self.summary.place(anchor="nw", x=280, y=100)
        self.sumry = ttk.Label(self.root)
        self.sumry.configure(text="Summary")
        self.sumry.place(anchor="nw", x=269, y=280)
        self.sumText = tk.Text(self.root, wrap=tk.WORD)
        self.sumText.configure(background="#c9c9c9", height=20, width=75)
        self.sumText.place(anchor="nw", x=15, y=314)
        self.urlLabel = ttk.Label(self.root)
        self.urlLabel.configure(text="URL")
        self.urlLabel.place(anchor="nw", x=269, y=650)
        self.urlEntry = ttk.Entry(self.root)
        self.urlEntry.place(anchor="nw", width=550, x=29, y=680)
        self.summarizeBut = ttk.Button(self.root, command=self.summarize)
        self.summarizeBut.configure(text="Summarize")
        self.summarizeBut.place(anchor="nw", x=247, y=710)
        self.root.configure(height=800, relief="flat", width=800)
        self.root.geometry("640x750")
        self.root.resizable(False, False)
        

        # Main widget
        self.mainwindow = self.root

    def run(self):
        self.mainwindow.mainloop()
    def summarize(self):
        url= self.urlEntry.get()
        #print(url)
        article= Article(url)
        article.download()
        article.parse()
        article.nlp()
        text=article.summary
        
        self.titleText.config(state='normal')
        self.authorText.config(state='normal')
        self.dateText.config(state='normal')
        self.sumText.config(state='normal')
        
        self.titleText.delete('1.0', 'end')
        self.titleText.insert('1.0',article.title)
        
        self.authorText.delete('1.0', 'end')
        self.authorText.insert('1.0',article.authors)
        
        self.dateText.delete('1.0', 'end')
        self.dateText.insert('1.0', article.publish_date)
        
        self.sumText.delete('1.0', 'end')
        self.sumText.insert('1.0',text)
        
        self.titleText.config(state='disabled')
        self.authorText.config(state='disabled')
        self.dateText.config(state='disabled')
        self.sumText.config(state='disabled')

if __name__ == "__main__":
    app = SummarizerguiApp()
    app.run()

Here is the error I am getting

Exception in Tkinter callback
Traceback (most recent call last):
  File "D:\ProgramFiles\anaconda3\lib\tkinter\__init__.py", line 1892, in __call__
    return self.func(*args)
  File "C:\Users\user\AppData\Local\Temp\ipykernel_12848\2706196857.py", line 78, in summarize
    self.dateText.insert('1.0', article.publish_date)
  File "D:\ProgramFiles\anaconda3\lib\tkinter\__init__.py", line 3743, in insert
    self.tk.call((self._w, 'insert', index, chars) + args)
_tkinter.TclError: wrong # args: should be ".!text3 insert index chars ?tagList chars tagList ...?"

Any help would be appreciated

0

There are 0 answers