首页 > 解决方案 > 使用 python 将 JSON 文件连接到 GUI 界面

问题描述

大家好,这是我的帖子,如果我不清楚我目前正在开发一个接口,该接口可以连接到一个 JSON 文件,该文件是一个更大项目的原型,目前我已经开发了看起来像这样的接口。目前我希望它根据输入的内容(例如 id、名称或来源)返回 JSON 中存在的一些文本。我的问题是我有 JSON 文件和下面存在的接口代码目前是编码的初学者我不确定用于将 JSON 文件连接到接口的代码,因此它会返回存在的文本在 JSON 字符串中。

我已经在网上寻找如何做到这一点,但似乎没有什么可找到的,我希望这里的任何人都可以提供帮助。

界面图片。[1]:https ://i.stack.imgur.com/Cszdl.png

接口代码提取:

from tkinter import *

window = Tk()
btn = Button(window, text="Submit", fg='blue')
btn.place(x=120, y=120)
txtfld=Entry(window, text=" ", bd=5)
txtfld.place(x=80, y=50)
txtfld=Entry(window, text=" ", bd=5)
txtfld.place(x=80, y=20)
txtfld=Entry(window, text=" ", bd=5)
txtfld.place(x=80, y=80)
lbl=Label(window, text="DatasetName", fg='red', font=("Helvetica", 8))
lbl.place(x=10, y=20)
lbl=Label(window, text="DatasetID", fg='red', font=("Helvetica", 8))
lbl.place(x=23, y=50)
lbl=Label(window, text="Source", fg='red', font=("Helvetica", 8))
lbl.place(x=23, y=80)
window.title='Database interface'
window.geometry("300x200+40+20")
window.mainloop()

Json 文件字符串:

{
   "datasetid":{
      "0":45
   },
   "name":{
      "0":"English indices of deprivation"
   },
   "source":{
      "0":"DfCLG: Department for Communities & Local Government"
   },
   "release_date":{
      "0":"2019-01-01"
   },
   "next_release_due":{
      "0":"2023-01-01"
   },
   "geo_ceil":{
      "0":"country"
   },
   "geo_floor":{
      "0":"ladistrict"
   },
   "year":{
      "0":true
   },
   "quarter":{
      "0":false
   },
   "month":{
      "0":false
   },
   "day":{
      "0":false
   },
   "week":{
      "0":false
   }
}

标签: pythonjson

解决方案


import json
file = open("json_file_here.json")#make sure they r in same location to do this
data=json.load(file)#stores data from file in data variable

然后从这里开始根据您的需要和使用迭代数据变量

txtfld.insert(0,"required filed here")

PS:-更改输入字段的名称以指定需要去哪里


推荐阅读