How do I add certain values from a json file to a python for loop and listbox?

Question:

I want to make a for loop that will add "Network Name, Network IP, Subnet, Inter Devs, and Hosts" to a label then take the "Usable IPs" and put them into a list box below the label. Does anyone know how I can do this?

I have put my json file and for loop below.

JSON:

{
  "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"
    }
  ]
}

Python:


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()

Appreciate the help in advance.

Asked By: Willy56

||

Answers:

You have initialized, but not started tkinter. The function to start tkinter is mainloop(), so you need to call self.win.mainloop() at the end of your __init__.

You will also need to create an instance of the class you created. So add NetAppHomePG() to the end of your program.

Answered By: nikost
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)
Answered By: Mehedi Khan
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.