英文:
Change the content of a ttkbootstrap ScrolledFrame
问题
我如何替换ttkbootstrap ScrolledFrame的内容?
我已经使用以下代码创建了滚动帧:
        for ndx, t in enumerate(sorted(tw, key=lambda x: x.created_at, reverse=True)):
            print(t.created_at)
            card = make_tweet_card(t, tweet_detail_scroller)
            card.grid(pady=5, row=ndx, column=0, sticky="W")
基于按钮点击,我需要清空ScrolledFrame并用不同的内容替换它。
如何实现这一目标?
英文:
How do I replace the content of a ttkbootstrap ScrolledFrame?
I have built the scrolled frame with this snippet:
        for ndx, t in enumerate(sorted(tw, key=lambda x: x.created_at, reverse=True)):
            print(t.created_at)
            card = make_tweet_card(t, tweet_detail_scroller)
            card.grid(pady=5, row=ndx, column=0, sticky="W")
Based on a button click I need to empty the ScrolledFrame and replace the content with different content.
How can I achieve this?
答案1
得分: 0
我发现我需要一个单独的内容列表,然后才能对其进行遍历以进行销毁。
代码段现在如下:
        if len(tweet_detail_scroller.children) > 0:
            content = [cw for cw in tweet_detail_scroller.children.values()]
            for cw in content:
                cw.destroy()
        for ndx, t in enumerate(sorted(tw, key=lambda x: x.created_at, reverse=True)):
            print(t.created_at)
            card = make_tweet_card(t, tweet_detail_scroller)
            card.grid(pady=5, row=ndx, column=0, sticky="W")
其中 tw 包含新的推文列表。
英文:
I found that I needed a separate list of the content before I could iterate through it for destruction.
The snippet is now:
        if len(tweet_detail_scroller.children) > 0:
            content = [cw for cw in tweet_detail_scroller.children.values()]
            for cw in content:
                cw.destroy()
        for ndx, t in enumerate(sorted(tw, key=lambda x: x.created_at, reverse=True)):
            print(t.created_at)
            card = make_tweet_card(t, tweet_detail_scroller)
            card.grid(pady=5, row=ndx, column=0, sticky="W")
where tw contains the new list of tweets.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论