Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to add a Tk GUI to a C program.

668 views
Skip to first unread message

Rajiv Mathews

unread,
Mar 28, 2006, 8:55:31 AM3/28/06
to
Hi,

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

Googie

unread,
Mar 28, 2006, 9:00:06 AM3/28/06
to

http://cpptk.sourceforge.net/

--
Pozdrawiam! (Greetings!)
Googie

Michael Schlenker

unread,
Mar 28, 2006, 9:17:27 AM3/28/06
to
Googie has some nice link posted, but depending on your program there
may be others.

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

Kevin Walzer

unread,
Mar 28, 2006, 9:28:04 AM3/28/06
to
The simplest way is just to create the GUI in Tk and have the various
widgets (buttons, entry, spinbox, etc.) collect user input, run the
commands provided by the C program via exec or pipes, and return output.
Nearly all of my programs work in this fashion (as a GUI to a
command-line tool or letting various command-line programs provide
under-the-hood functionality).

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

Cameron Laird

unread,
Mar 28, 2006, 10:08:02 AM3/28/06
to
.
.
.
A more general introduction to the topic appears
at <URL: http://wiki.tcl.tk/4798 >.

Robert Heller

unread,
Mar 28, 2006, 10:32:46 AM3/28/06
to
"Rajiv Mathews" <raji...@gmail.com>,
In a message on 28 Mar 2006 05:55:31 -0800, wrote :

"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



Cameron Laird

unread,
Mar 28, 2006, 5:08:02 PM3/28/06
to
In article <60250$4429571e$cb248f0$38...@news.news-service.com>,
Robert Heller <hel...@deepsoft.com> wrote:
.
.

.
>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.
.
[details]
.
.
Nice descriptions. There are, of course, several variations
on these themes. One that I think deserves emphasis, simply
because it's simultaneously so little-understood and potenti-
ally life-altering, is out-of-process communication. Arjen,
for example, has several times, I believe, enabled a Fortran
application for scripting by opening a simple socket channel
between the Fortran process and a Tk instance; people working
in Lisp and several other languages do this as well, and have
even standardized on it.

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.

Arjen Markus

unread,
Mar 29, 2006, 1:41:28 AM3/29/06
to
Well, as a matter of fact, we use that technique in several of our
commercial
products on Windows to get around some awkward limitations of
DOS-boxes!
The technique works without any adjustments to the program that is
being
controlled and it is completely indifferent to the language the program
was
written in.

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

bob findlay

unread,
Mar 30, 2006, 4:09:47 PM3/30/06
to
Rajiv Mathews wrote:

> 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

David Gravereaux

unread,
Mar 30, 2006, 4:59:38 PM3/30/06
to
Robert Heller wrote:

> 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

0 new messages