preveen-stack / nodejs

0 stars 0 forks source link

python gui example with tkinter #24

Open preveen-stack opened 1 month ago

preveen-stack commented 1 month ago
import tkinter as tk
import json

class SolarModuleEditor(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.title("Solar Module Editor")
        self.geometry("400x300")

        entries = [
            ("Name:", 0),
            ("Pmax (W):", 1),
            ("Voc (V):", 2),
            ("Isc (A):", 3),
            ("FF:", 4),
            ("Rs (Ohms):", 5),
            ("Rsh (Ohms):", 6),
            ("Temperature Coefficient:", 7),
            ("Light-Induced Degradation:", 8)
        ]

        for label, row in entries:
            tk.Label(self, text=label).grid(row=row, column=0)
            tk.Entry(self).grid(row=row, column=1)

        tk.Button(self, text="Save", command=self.save).grid(row=9, column=0, columnspan=2, pady=10)

    def save(self):
        module = {}
        entries = self.grid_slaves()

        for i in range( 1, len(entries), 2):
            module[entries[i+1].cget("text").strip(":")] = entries[i].get()

        with open("module.json", "w") as f:
            json.dump(module, f)

        print(f"written : {module}")
app = SolarModuleEditor()
app.mainloop()
preveen-stack commented 1 month ago
image