首页 > 解决方案 > 使用带有 JSON 数据的文本文件并在 Tkinter 中使用它

问题描述

我使用了一个使用 python 的 API,调用“NewsAPI”来获取我需要的所有最新消息,我实际上已经将它保存到一个名为“NewsAPI.txt”的文本文件中。

我的代码是:

    import json 
    import requests


    def newsAPI():
        url = ('https://newsapi.org/v2/everything?' #API URL
               'q=procurement AND tender&' #keywords on procurement AND tender
               'sortBy=popularity&' #Sort them by popularity
               'apiKey=***') #Personal API key

        # GET
        response = requests.get(url)

        #storing the output into variable "results"
        results = response.json()
        # save the JSON output into a txt file for future usage
        with open("NewsAPI.txt", "w") as text_file:
            json.dump(results, text_file)

调用后json.dump,它会保存到我提到的“NewsAPI.txt”文件中。但是我无法将它放入 Tkinter 中的树视图中,还是我使用了错误的小部件来显示它们?

输出数据: JSON 数据的输出

标签: pythonjsontkinter

解决方案


推荐阅读