I have a C program, to which I want to add a GUI.
A front end to the program.
Someone told me Tk can make my task easy.
However I haven't been able to find any good help.
Can anyone point me to some simple good tutorials
on the same.
I want basic GUI functionality, nothing special and
fancy. Just the ability to display output to the user
and a couple of buttons to get input maybe.
Thanx for any help.
Rajiv Mat
There are various approaches, depending on the structure and type of
your program, some are easier, some are more involving.
This page has some pointers/examples:
http://wiki.tcl.tk/2074
Michael
You can get more complicated and actually embed the Tcl interpreter in
your program, but if you need to move quickly, this is the way to go.
--
Kevin Walzer
iReveal: File Search Tool
http://www.wordtech-software.com
"M> Hi,
"M>
"M> I have a C program, to which I want to add a GUI.
"M> A front end to the program.
"M> Someone told me Tk can make my task easy.
"M> However I haven't been able to find any good help.
"M> Can anyone point me to some simple good tutorials
"M> on the same.
"M> I want basic GUI functionality, nothing special and
"M> fancy. Just the ability to display output to the user
"M> and a couple of buttons to get input maybe.
"M> Thanx for any help.
There are three approaches:
1) Using exec and/or popen (open "|..." .) with a self-contained CLI C
program.
2) Making the C 'program' into a (shared) library and using SWIG to
create an interface layer.
3) Writing C/C++ code that uses the Tcl_ and Tk_ API, complete with a
Tcl_Main() function.
How much of an existing main() do you already have? If 'not much' and
if your C code is pretty much a library of functions, that option 2 is
probably the best option. If your C code is a single straight-through
process with an existing command line argument set (or a simple Q&A
main()), then option 1 is a good solution. *I* would recommend against
option 3, simply because it will be harder and may end up tied too tightly
to a particular Tcl/Tk release.
For option 1, your C program would be a complete C program, complete
with a main() function, built on its own. The main() would handle argc,
argv parameters (ala int main(int argc, char *argv[])) and possible send
output to stdout (ala printf()) and have a proper exit status (0 for
success, non-0 for errors). With this sort of interface, you would
write a pure Tcl/Tk GUI (there are various extensions that might be
helpful, such as BWidget or snit), which would eventually have some sort
of 'run' button or menu item that would do something like:
if {[catch [list open "|cprog arg1 arg2 ..." r] pipeIn]} {
tk_messageBox -type ok -icon error -message "Error forking cprog: $pipeIn"
} else {
fileevent $pipeIn readable [list logoutput $pipeIn]
}
...
proc logoutput {pipe} {
if {[gets $pipe line] < 0} {
if {[catch [list close $pipe] exitStatus]} {
tk_messageBox -type ok -icon error -message "CProg crashed: $exitStatus"
}
} else {
# LogWindow is something like a text widget
global $LogWindow
$LogWindow insert end "$line\n"
$LogWindow see end
}
}
For option 2, where your main() either does not exist (yet) or is
minimal (just enough for testing, etc.), you would use SWIG to process
a C or C++ header file (best if you use the original C or C++ headers,
with some #if[n]def SWIG/#endif lines to 'hide' stuff the Tcl/Tk does
not need to deal with), to create some C or C++ to expose your function
(or class) library as Tcl commands. You would then build your C or C++
code into a loadable shared module (.so or .dll), which would get
loaded into Tcl/Tk system with the load command. If you build it with
-DUSE_TCL_STUBS (highly recommended), you won't be locked to a
particular release of Tcl/Tk. You would then write a 'main' program in
Tcl/Tk, which would call your C/C++ library functions in response to
GUI events.
"M>
"M> Rajiv Mat
"M>
"M>
Robert Heller -- 978-544-6933
Deepwoods Software -- Linux Installation and Administration
http://www.deepsoft.com/ -- Web Hosting, with CGI and Database
hel...@deepsoft.com -- Contract Programming: C/C++, Tcl/Tk
Perhaps even more dramatic is COM access under Windows. Many
Windows applications (whether their owners realize it or not)
are COM servers, and Tcl extended with tcom controls these to
gloriously good effect.
There are a few caveats:
- If the program reads from the keyboard, you will need to take care of
that
on the Tcl side - but that is no big deal
- If the program starts another program, that will create a new
DOS-box,
unless you take measures (you will need to use the Windows API to
create a process that inherits the standard input/output channels,
instead
of simply calling system() or something similar)
- If the program is a Fortran program that uses the PAUSE statement,
you may consider removing that statement:
- It is a leftover from dark days when computers were actually human
beings
(well, slightly later than that :))
- The effect is somewhat bewildering, as I can tell from personal
experience:
a rapid succession of DOS-boxes, so rapid that the only reasonable
way
to the program is to turn off the computer.
But seriously: the method is very very worthwhile!
Regards,
Arjen
> Hi,
If you are able to "adjust" your C program you might want to try the very
easy API and Tcl/Tk hooks offered by the SIMPL project at:
https://sourceforge.net/projects/simpl
bob
> There are three approaches:
>
> 1) Using exec and/or popen (open "|..." .) with a self-contained CLI C
> program.
A variation on that is to use Expect and Tk combined.
http://expect.nist.gov