-1

This is Python version 3.6.2

Below is my method of getting news through the Google New's RSS feed. I am trying to delete the frame so that when the method is called again the text is erased and replaced with new text. How would I go about removing the frame? I would like to delete it so when it is called only 5 news articles are displayed instead of stacking upon each other. I currently have a Bottom Frame and inside that I have a Bottom Left Frame, a News Frame which holds all the news article labels, and a Newspaper Image frame, which contains all the images. I am trying to delete the News Frame so when I call my get_news method it's a fresh frame with no labels. Any help would be appreciated. This is the GUI of the project

def get_news():
    try:
        feed = feedparser.parse(google_news_url)

        for post in feed.entries[0:5]:
            newspaper_image = Label(frame_newspaper, bg='black', fg='white')
            newspaper_image.configure(image=photo)
            newspaper_image.icon = photo

            label_news = Label(frame_news, bg='black', fg='white', font=newsFont)
            label_news['text'] = post['title']
            newspaper_image.pack(side=TOP, anchor=W)
            label_news.pack(side=TOP, anchor=W)
        frame_news.after(600000, get_news)

    except Exception as e:
        traceback.print_exc()
        print("Error: %s. Cannot get news." % e)
  • 4
    I suggest that you _don't_ destroy and rebuild the Frame, just update its contents. You can change the text / image in a Label by calling its `.config` method. Or associate a StringVar with the Label, and `.set` a new value to that StringVar. – PM 2Ring Oct 13 '17 at 03:42
  • Your question doesn't include a question. You've said what you want but haven't explained what you need help with. – Bryan Oakley Oct 13 '17 at 11:04
  • I think if I just make 5 labels and then update the text every time I loop it would be easier to manage, however it is just messier. Currently I make 5 labels every time I loop however, they remain and I make 5 more labels when I loop again. – PWRxPSYCHO Oct 13 '17 at 13:12

1 Answers1

0

In the beginning of the method you have to get the children of the frames and destroy them. I did it like this:

    for widget in frame_news.winfo_children():
        widget.destroy()
    for i in frame_newspaper.winfo_children():
        i.destroy()

This clears all the labels that were created during the loop. Then when the loop is called new labels are created taking the place of the old ones.