如何将JSON文件中的某些值添加到python for循环和列表框中?

gg58donl  于 2022-12-30  发布在  Python
关注(0)|答案(2)|浏览(111)

我想做一个for循环,将"网络名称,网络IP,子网,Inter Devs和主机"添加到标签中,然后将"可用IP"放入标签下面的列表框中。有人知道我如何做吗?
我把我的json文件和for循环放在下面。
我的天啊

{
  "networks": [
    {
      "Network Name": "wha",
      "Network IP": "10.0.1.0",
      "Subnet": "255.255.255.240",
      "Usable IPs": [
        "10.0.1.1",
        "10.0.1.2",
        "10.0.1.3",
        "10.0.1.4",
        "10.0.1.5",
        "10.0.1.6",
        "10.0.1.7",
        "10.0.1.8",
        "10.0.1.9",
        "10.0.1.10"
      ],
      "Intermediary Devices": "1",
      "Hosts": "9"
    },
    {
      "Network Name": "newnet",
      "Network IP": "192.168.0.0",
      "Subnet": "255.255.255.240",
      "Usable IPs": [
        "192.168.0.1",
        "192.168.0.2",
        "192.168.0.3",
        "192.168.0.4",
        "192.168.0.5",
        "192.168.0.6",
        "192.168.0.7",
        "192.168.0.8",
        "192.168.0.9",
        "192.168.0.10"
      ],
      "Intermediary Devices": "2",
      "Hosts": "8"
    },
    {
      "Network Name": "myNet",
      "Network IP": "192.168.0.12",
      "Subnet": "255.255.255.0",
      "Usable IPs": [
        "192.168.0.13",
        "192.168.0.14",
        "192.168.0.15",
        "192.168.0.16",
        "192.168.0.17",
        "192.168.0.18",
        "192.168.0.19",
        "192.168.0.20"
      ],
      "Intermediary Devices": "10",
      "Hosts": "200"
    }
  ]
}

巨蟒:

import json
import ctypes
from tkinter import *
from PIL import Image, ImageTk

ctypes.windll.shcore.SetProcessDpiAwareness(1)

class NetAppHomePG:

    def __init__(self):

        self.win = Tk()
        self.win.geometry("3800x2000")
        self.win.title("NetApp")

        self.TabsFrame = Frame(self.win)

        self.r=0
        self.c=1

        self.TabsFrame.grid(sticky=W)
        self.MyNetsCanvas = Canvas(self.TabsFrame, height=1500, width=2500)
        self.MyNetsCanvas.grid(row=0, rowspan=3, column=1, columnspan=2, pady=200)
        self.MyNetsFrame = Frame(self.MyNetsCanvas, height=10000, width=500)
        self.MyNetsCanvas.create_window(0, 0, window=self.MyNetsFrame, anchor=NW)

        with open("user.json") as f:
            self.data = json.load(f)
            self.temp = self.data['networks']
            
        for self.network in self.data['networks']:
            self.MyNets = Frame(self.MyNetsFrame)
            self.MyNets.grid(row=self.r, column=self.c, pady=40)
            if (self.c + 1) % 2 == 0:
                self.r += 1
                self.c = 1
            else:
                self.c += 1
            for key, value in self.network.items():
                Label(self.MyNets, text=f'{key}: {value}', anchor="w", bg="white", fg="black", font=("Roboto", 14), width=100).grid(padx=150)

        self.win.mainloop()

NetAppHomePG()

提前感谢你的帮助。

jk9hmnmh

jk9hmnmh1#

import json
try:
    with open('user.json') as json_file:
        json_response = json.load(json_file)
        for network in json_response.get('networks', ''):
            network_name = network.get('Network Name', '')
            network_ip = network.get('Network IP', '')
            subnet = network.get('Subnet', '')
            intermediary_devices = network.get('Intermediary Devices', '')
            hosts = network.get('Hosts', '')
            for usable_ip in network.get('Usable IPs', []):
                print(usable_ip)
except FileNotFoundError as e:
    print(e)
2nc8po8w

2nc8po8w2#

你已经初始化了,但是还没有启动tkinter,启动tkinter的函数是mainloop(),所以你需要在__init__的末尾调用self.win.mainloop()
你还需要创建一个你创建的类的示例,所以把NetAppHomePG()添加到你的程序的末尾。

相关问题