[Tutor] Long post: How to design a Python program to generate GUI elements in a proprietary scripting language.

Steven D'Aprano steve at pearwood.info
Mon Apr 29 02:47:24 CEST 2013


On 29/04/13 06:05, boB Stepp wrote:
> At work I use a commercial program for radiation therapy planning.
> This program has an extensive built-in, proprietary scripting language
> that is accessible to the user.

Does this have a name? It's a bit clumsy to refer to "proprietary
scripting language" each time. I expect your program's documentation
refers to the scripting language by name.

Also, knowing the name would allow people to google for it. Who knows,
perhaps someone has already written something that solves your problem.

For lack of a better name, I'm going to call this GammaScript, since
it's involved in radiation therapy.

A full-blown GUI designer written in Python would be a lot of work, but
you might find that most of your GUIs follow a standard pattern, say:

1 window
1 heading
1 text field
OK and Cancel buttons
3-5 radio buttons
3-7 checkboxes
1-2 pop-up menus


in which case you can take such a script and turn it into a template.
Start off with a working GammaScript GUI script. I have no idea what that
looks like, so I'll just make something up:


create new window "Main Window"
add field "Label" to window "Main Window" at 20, 20
set the text of field "Label" to "Heading"
add field "Text" to window "Main Window" at 20, 40
set the size of field "Text" to 300, 400
set the scrollbar of field "Text" to true
add button "OK" to window "Main Window" at 270, 420
etc.


You can turn that into a template:


create new window $WINDOW_NAME
add field $LABEL_NAME to window $WINDOW_NAME at 20, 40
set the text of field $LABEL_NAME to $LABEL
add field $TEXT_NAME to window $WINDOW_NAME at 20, 20
...


Save it as a plain text file, call it "template.txt". Then have your
Python script read template.txt, query the user in some fashion for
the substitution values, and save the result as a GammaScript file.

Querying the user need not be interactive. You could take a
combination of approaches:

- read from a config file
- accept values from the command line
- interactively ask the user for values
- all of the above


The final step is for the user to hand-edit the generated GammaScript
file, before running it and seeing what breaks :-)

(Hopefully not anyone's radiation therapy...)


I recommend the string template safe_substitute method rather than
%s or {} formatting, since that will leave unused $TEMPLATES in the
file where you can see them and edit them by hand if needed.


If that works well, it might solve your problem. If not, you can
then take a few steps closer to a full-blown GUI builder by using
a custom mini-language to describe the GUI you want. E.g. you might
have something like this:


win = window("Main Window")
label = field(win, "Label", x=20, y=20, text="Heading")
text = field(win, "Text", x=20, y=40, size=(300, 400), scrollbar=True)
ok = button(win, x=text.right - 40, y=text.bottom + 30)
win.generate("mygammascript.gs")

which then generates the GammaScript code shown above. Because it's
Python, you can use normal Python constructs like for-loops:

for name in ("Alpha", "Beta", "Gamma"):
     x = checkbox(win, x=..., y=..., state=False)


A text interface like this is still a lot of work, but it's much less
work than a graphical interface, and nearly as useful.


But don't underestimate how much work this will be. This is a big
project.



-- 
Steven


More information about the Tutor mailing list