如何从JSON多维附加动态下拉列表

时间:2019-09-10 05:16:59

标签: python tkinter python-3.7.4

如何从json多维附加dropdownlist附加showtext和值。

ShowText = DId 值= LocId

from functools import partial
from tkinter import *
from tkinter import messagebox

def ShowValue(val) :
    messagebox.showinfo('Message',val)
    return

def CallFunc():
    data = [{'DId': '158d0002f2f156', 'LocId': '9', 'status': '1', 'DateRec': '2019-08-23 15:34:43'},
           {'DId': '158d0002f2f431', 'LocId': '19', 'status': '1', 'DateRec': '2019-08-23 22:29:58'},
           {'DId': '158d0002f2f321', 'LocId': '12', 'status': '1', 'DateRec': '2019-08-23 16:15:56'},
           {'DId': '158d0002f2f544', 'LocId': '13', 'status': '1', 'DateRec': '2019-08-27 17:53:48'}]
    return data

gui = Tk()
gui.geometry("500x200")
gui.title("Test Temperature")
gui.configure(background="white")

lab00 = Label(gui, text="Select Devices : ", bg="white")
lab00.grid(row=0, column=0)

option = StringVar()
Value = CallFunc()
for i in range(len(Value)) :
    menu = OptionMenu(gui,option,Value[i]['DId'])

menu.grid(row=0,column=1)

btn = Button(gui, text="Show",command=partial(ShowValue,option.get()))
btn.grid(row=4,column=1)

gui.mainloop()

1 个答案:

答案 0 :(得分:0)

您可以通过列表理解来获取值:

option = StringVar()
menu = OptionMenu(gui, option, *[i.get("DId") for i in CallFunc()])
menu.grid(row=0, column=1)

对于按钮命令,您可以改用lambda

btn = Button(gui, text="Show", command=lambda: ShowValue(option.get()))

要获取json文件的值,请在ShowValue函数中进行查找:

def ShowValue(val) :
    for i in CallFunc():
        if val in i.values():
            message = i.get("LocId")
            messagebox.showinfo('Message',message)
            return