No Huddle Offense

"Individual commitment to a group effort-that is what makes a team work, a company work, a society work, a civilization work."

Generating Tkinter GUIs from JSON

April 21st, 2011 • 2 Comments

Source available on GitHub

Yesterday I posted something about creating Tkinter based GUIs via an XML. Thanks to python and all its build in features (like JSON support) it was a piece of cake to rewrite it an now you can create Tkinter GUIs from a JSON definition. Just a few lines (ca 25 lines needed to be changed) later and this request was fulfilled:

To create the same GUI as in the last post the JSON file should look like:

{
    "Frame":{
        "LabelFrame":{
            "text":"Hello World",
            "column": 0,
            "columnspan": 2,
            "row": 0,
            "Label":{
                "text":"Entry:",
                "column": 0,
                "row": 0
            },          
            "Checkbutton":{
                "name":"check",
                "text":"checkbutton",
                "column": 1,
                "row": 0
            },
            "Entry":{
                "name":"entry",
                "text":"checkbutton",
                "width":"30",
                "bg":"gold",
                "column": 0,
                "row": 1,
                "columnspan": 2
            }
        },
        "Button":[
            {
                "name":"ok",
                "text":"OK",
                "column": 0,
                "row": 1
            },
            {
                "name":"cancel",
                "text":"Cancel",
                "column": 1,
                "row": 1
            }
        ]
    }
}

And for sure: the sample code from yesterday – didn’t change 🙂 Again: you gotta love python! And right now the JSON UI definitions are nicer so I might follow this approach for the future.

Also nice about this approach is that within the JSON or XML file you define everything for the Tkinter widgets. Every value of the configuration of an Tkinter widget can be defined in the files. For example all these values could be put in the file for the ‘Entry’ Tkinter widget. Nice ain’t it?

2 responses to “Generating Tkinter GUIs from JSON”

  1. […] with my Solaris installation is that it has slightly older TCK/TK libs. Since I’m currently playing around with some Tkinter stuff and python I needed a newer version to test the TreeView and […]