python - 使用 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
}
}
解决方案
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:-更改输入字段的名称以指定需要去哪里
推荐阅读
- javascript - 无法使用 Node.js 将表单数据发送到 MongoDB 数据库
- r - R中的日期和时间减少
- azure - 使用 AAD 的多租户应用到应用授权
- rtos - BeagleBone Green 上的 QNX RTOS
- javascript - 如何从 HTML/Javascript 中的函数访问 URL?
- typescript - 有没有办法阻止 TypeScript 简化声明文件中的 `keyof` 语句?
- linux - 用于返回多个数据文件的 date_time 列的范围(最小值和最大值)的 Shell 脚本
- javascript - Webpack Bundle:(看似随机)参考错误:未定义窗口
- promise - 蓝鸟承诺已创建但未返回警告
- foxpro - Foxpro 含义混淆