Pragana's Tcl Guide


Old notes


Forms for a database application

As I promised last week, here are some examples of stored forms. Our first example will be a form control panel, because with it we can manage easier other forms. The idea is to draw a form like the figure below and attach the bindings and commands shown in Visual Tcl. Then execute a TkCon or similar program to send a command to capture the form as drawn in vtcl (visual tcl) and save it in our database. We can even execute the send commands from a xterm running wish, but with TkCon is easier and less troublesome.
Let us see in detail what have to be done.

The widgets in the design above are: .formcontrol.title (label), .formcontrol.f1 (frame for the radio buttons), .formcontrol.f1.ext and .formcontrol.f1.loc (radiobuttons with -variable formcapture and -value 1 and 0, respectively), .formcontrol.f2 (frame for the buttons, packed at bottom), .formcontrol.f2.saveform, .formcontrol.f2.exec and .formcontrol.f2.quit (buttons), .formcontrol.f3 (frame for the entry and label), .formcontrol.f3.formlab (label), and .formcontrol.f3.name (entry for the form name).

This seems complex at first, but the names don't matter much if you can change them in the commands given next.
We must be sure the radiobuttons have the same variable formcapture with value 1 for the labeled "external" and 0 for the other.
The toplevel (.formcontrol) will have a binding to initialize this variable:

	bind .formcontrol <Activate> {set formcapture 0}
Finally, the buttons will have the following commands:
buttoncommand
.formcontrol.f2.saveformstore_form [.formcontrol.f3.name get] $formcapture
.formcontrol.f2.execdoForm [.formcontrol.f3.name get]
.formcontrol.f2.quitexit

After drawing this form in vtcl, select TEST mode (a small label where it's written EDIT otherwise). You must have the PostgreSQL server running with you as a user. You must also have created the 2 tables discussed last week: frm_form and frm_objects.
Then execute TkCon and source this text file to setup some variables, load the postgres/tcl interface and open the database (loja).


set kstore(dir) /path/to/where/your/libraries/are
set auto_path [linsert $auto_path 0 $kstore(dir)/lib]
db_open loja
load libpgtcl.so
set conn [pg_connect loja]
wm withdraw .

Then you're ready to save your newly created form. Execute:

store_form  formcontrol 1
The first arg is the form name, the second is "true" for the capturing of vtcl toplevel drawn. Answer the question for a query with OK. We are not going to do any query with this form.
After this, bingo! You have your form stored in the database. Better yet: new forms could only be drawn and saved by this "control panel" form. Save the 6 lines given above as controlpanel.tcl and include at it's end:
	doForm formcontrol
to invoke your control panel. Congratulations!

The library directory must have the previously given inputbox.tcl and form.tcl files (the 2 last weeks).


Back Home