Rella
Rella

Reputation: 66945

C++/TCL how to invoke tk?

So I want to exequte TCL code inside my programm. I want it to be capable of reaching some of my C++ functions and classes. So I use C++/TCL. All general TCL to C++ binding works fine for me. But now I want to have some little UI on top (so I have lot on general TCL code that interacts with my app and now I want to add Tk gui to it.) How to create simple tk button inside of the window with some name I want using C++/TCL? I do not want to use C++/TK at all.

Update What have I tried:

Put all Tk (.tcl) files into ../../tk folder and try this... it fails

#include <string>
#include "cpptcl.h"

int main(int argc, char *argv[])
{
    std::string script = 
        "package require Tcl 8.5\n"
        "set auto_path [linsert $auto_path 0 [file join [file dirname [info script]] ../../tk]]\n"
        "package require Tk 8.5\n";
    Tcl::interpreter tcl_interpreter;
    tcl_interpreter.eval(script);
    return 0;
}

Also I tried stuff like

#include <string>
#include "cpptcl.h"
int main(int argc, char *argv[])
{
    std::string script = 
        "package require Tcl 8.5\n"
        "foreach tkFile [glob -nocomplain -directory ../../tk *] {\n"
        "    source $tkFile\n"
        "}\n"
        "package require Tk 8.5\n";
    Tcl::interpreter tcl_interpreter;
    tcl_interpreter.eval(script);
    return 0;
}

Which also fails.

Also I tried to put tk85.dll near to my app and call

#include <string>
#include "cpptcl.h"
int main(int argc, char *argv[])
{
    std::string script = 
        "package require Tcl 8.5\n"
        "load tk85.dll \n";
    Tcl::interpreter tcl_interpreter;
    tcl_interpreter.eval(script);
    return 0;
}

and this

#include <string>
#include "cpptcl.h"
int main(int argc, char *argv[])
{
    std::string script = 
        "package require Tcl 8.5\n"
        "interp create slave \n"
        "load {} Tk slave \n"
    ;
    Tcl::interpreter tcl_interpreter;
    tcl_interpreter.eval(script);
    return 0;
}

as described here. this also fails.

This

#include <string>
#include "cpptcl.h"

int main(int argc, char *argv[])
{
    std::string script = 
        "package require Tcl 8.5\n"
        " interp create a\n"
        " a eval {set argv {-display :0}; package require Tk; button \".b\" -text \"Say Hello\"; pack \".b\" -padx 20 -pady 6;}\n"
        ;
    Tcl::interpreter tcl_interpreter;
    tcl_interpreter.eval(script);
    std::cin.get();
    return 0;
}

compiles and runs but does not show ani window or anething else.(

What to do? How to load Tk?

Upvotes: 1

Views: 1324

Answers (1)

Ben Voigt
Ben Voigt

Reputation: 283644

You haven't said what fails, but this isn't going to work. To have a GUI, you need an event loop.

I'd recommend a different approach: make your C++ code into a Tcl-loadable module, then your script can run under wish and use both Tk and your objects implemented in C++.

Upvotes: 1

Related Questions