简体   繁体   English

在方法中销毁 tkinter 框架

[英]Destroy tkinter frame within method

This is Python version 3.6.2这是 Python 版本 3.6.2

Below is my method of getting news through the Google New's RSS feed.下面是我通过 Google New 的 RSS 提要获取新闻的方法。 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.我想删除它,所以当它被调用时,只显示 5 篇新闻文章,而不是相互叠加。 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.我正在尝试删除新闻框架,因此当我调用 get_news 方法时,它是一个没有标签的新框架。 Any help would be appreciated.任何帮助,将不胜感激。 This is the GUI of the project这是项目的GUI

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)

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.然后,当循环被调用时,会创建新标签来代替旧标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM