Skip to main content

A Tkinter form dialog generator.

Project description

TkGen

About

tkgen is a python package that automatically generate a Tkinter form window from a Pyskema schema. It provide a simple interface to initialize the window and collect the data inputed by user. The data is provided as python native objects that would be valid regarding the schema. It depends on tkinter and Pyskema.

Installation

Recommended: Use pip install tkgen.

Manual: Clone this project. Run pip install . in this folder.

Usage

The main entrypoint of this package is the function tkgen.make_form. Here is a minimal exemple of its usage:

from tkgen import make_form
from pyskema import Node, AtomType

schema = Node.of_record({
    "a": Node.of_atom(AtomType.INT),
})

win = make_form(schema, print)
win.mainloop()

The first parameter of make_toplevel (the model) is the schema that define the form. The second parameter (the callback) is a function to be called when the form is submitted. The callback is passed a single argument which is either None (if the user pressed cancel) or the data inputed. This dictionary mirror the structure of the model.

Extracting data

Once the form is filled, you want to access its data. This is done through the callback parameter of make_form. This callback is an arbitrary function you should provide that will receive the data in the form of a dictionary.

Here is a simple example of saving the data in an arbitrary json file:

import json
from tkgen import make_form
from pyskema import Node, AtomType

model = Node.of_record({
    "filename": Node.of_atom(AtomType.STR),
    "Plumbus": Node.of_record({
        "number of schleem": Node.of_atom(AtomType.STR),
        "length of dinglepop": Node.of_atom(AtomType.FLOAT),
        "color of fleeb": Node.of_atom(AtomType.OPTION, [
            "pink",
            "red",
            "octarine",
        ]),
    }),
})

def save_data(result):
    filename = result["filename"]
    data = result["Plumbus"]
    with open(filename, "w") as f:
        json.dump(f, data)

win = make_form(model, save_data)
win.mainloop()

Loading data

You may want to be able to load back some data from a previous instance of the form. This is possible thanks to the init_data optional parameter. For simplicity it is also possible through the optional data parameter of make_form.

If you were to use the previous example and save a file named plumbus.json, the following example would load data from the json file and produce a filled form identical to what it looked when you saved the file.

import json
from tkgen import make_form
from pyskema import Node, AtomType

model = Node.of_record({
    "filename": Node.of_atom(AtomType.STR),
    "Plumbus": Node.of_record({
        "number of schleem": Node.of_atom(AtomType.STR),
        "length of dinglepop": Node.of_atom(AtomType.FLOAT),
        "color of fleeb": Node.of_atom(AtomType.OPTION, [
            "pink",
            "red",
            "octarine",
        ]),
    }),
})

with open(filename, "r") as f:
    data = json.load(f)

saved_data = {
    "filename": filename,
    "Plumbus": data,
}

def save_data(result):
    filename = result["filename"]
    data = result["Plumbus"]
    with open(filename, "w") as f:
        json.dump(f, data)

win = make_form(model, save_data, init_data=saved_data)
win.mainloop()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tkgen-0.2.2.tar.gz (16.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tkgen-0.2.2-py3-none-any.whl (17.8 kB view details)

Uploaded Python 3

File details

Details for the file tkgen-0.2.2.tar.gz.

File metadata

  • Download URL: tkgen-0.2.2.tar.gz
  • Upload date:
  • Size: 16.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.9

File hashes

Hashes for tkgen-0.2.2.tar.gz
Algorithm Hash digest
SHA256 44f565740fab6818f5e1fe4358a532bb21854548fb88f19d7d4aad68ac696707
MD5 0a5645812168bae62c3e88d9cc80ec56
BLAKE2b-256 3e4f046947e901f6a3e6b30e360b8f033a831f29212683d5e1dd15a7c17ad3ed

See more details on using hashes here.

File details

Details for the file tkgen-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: tkgen-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 17.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.9

File hashes

Hashes for tkgen-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 785d944a8bba94ab2e58239872dd2a7fd58741283f701aa00345f446ec802a75
MD5 f4018609c9463a0d4393d508ab77d164
BLAKE2b-256 5da9436349254ccb5ec696f753c89f3000ceeb3a14f6e053ab97baeebfb80f83

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page