如何在同一个程序中同时使用C ++ / TCL和C ++ / TK?

时间:2011-07-05 18:20:48

标签: c++ user-interface function tcl tk

通过以下API为我们提供易于管理的TCL C ++函数和类,我很高兴C++/TCL

#include "cpptcl.h"
#include <iostream>
#include <string>
using namespace std;
using namespace Tcl;
void hello()
{
     cout << "Hello C++/Tcl!" << endl;
}
int main()
{
     interpreter i;
     i.def("hello", hello);
     string script = "for {set i 0} {$i != 4} {incr i} { hello }";
     i.eval(script);
}

与此同时,在C++/Tk处理系统事件循环与api类似

非常棒
#include <string>
#include "cpptk.h"

int main(int argc, char *argv[])
{
    std::string script = 
        "package require Tcl 8.5\n"
        "package require Tk 8.5\n"
        "ttk::button \".b\" -text \"Say Hello\"\n"
        "pack \".b\" -padx 20 -pady 6\n"
        ;
    Tk::details::Expr(script, true);
    Tk::runEventLoop();
}

因此,您可以看到一个非常适合创建另一个用于GUI渲染的API。

我想找到一种方法来混合它们,例如使用这样的代码:

#include "cpptcl.h"
#include "cpptk.h"
#include <iostream>
#include <string>
using namespace std;
using namespace Tcl;
void hello()
{
     cout << "Hello C++/Tcl!" << endl;
}
int main()
{
     interpreter i;
     i.def("hello", hello);
     string script = 
        "package require Tcl 8.5\n"
        "package require Tk 8.5\n"
        "ttk::button \".b\" -text \"Say Hello\" -command hello \n"
        "pack \".b\" -padx 20 -pady 6\n"
        ;
    Tk::details::Expr(script, true);
    Tk::runEventLoop();
}

如何做到这一点?如何混合使用C ++ / TCL和C ++ / Tk?

Updete:

所以我们已经做到了。需要一些CPP / TCL和CPP / Tk sorurce代码修复,请参阅our svnmy answer for example of use

3 个答案:

答案 0 :(得分:2)

您可以使用UI回调来执行复杂的操作,包括运行其他Tcl代码:

#include "cpptcl.h"
#include "cpptk.h"
#include <iostream>
#include <string>
using namespace std;
using namespace Tcl;

class TclHost
{
  interpreter i;
  static TclHost* singleton;

  static void hello()
  {
     cout << "Hello C++/Tcl!" << endl;
  }

  static void runScript()
  {
     singleton->i.def("hello", &TclHost::hello);
     string script = "for {set i 0} {$i != 4} {incr i} { hello }";
     singleton->i.eval(script);
  }

public:
  int main()
  {
     singleton = this;
     i.def("runscript", &TclHost::runScript);
     string script = 
        "package require Tcl 8.5\n"
        "package require Tk 8.5\n"
        "ttk::button \".b\" -text \"Run a script\" -command runscript\n"
        "pack \".b\" -padx 20 -pady 6\n"
        ;
    Tk::details::Expr(script, true);
    Tk::runEventLoop();

    return 0;
  }
};

TclHost* TclHost::singleton;

int main(void)
{
  TclHost().main();
}

您还需要查看Tcl / Tk事件循环支持的其他回调,包括计时器和文件I / O.

答案 1 :(得分:0)

所以we have done it...) here is our svn with updated, working C++/Tk and C++/TCL,修补了彼此的工作,纠正了官方sourceforge网站上的一些错误。

以下是代码示例启发by Ben

#include "cpptcl/cpptcl.h"
#include "cpptk/cpptk.h"
#include <iostream>
#include <string>
Tcl::interpreter i(Tk::getInterpreter());
void hello()
{
    std::cout << "Hello C++/Tcl!" << std::endl;
}
void runScript()
{
    i.def("hello", hello);
    std::string script = "for {set i 0} {$i != 4} {incr i} { hello }";
    i.eval(script);
}
int main(int argc, char *argv[])
{

    i.def("runscript", runScript);
    std::string script = 
        "package require Tcl 8.5\n"
        "package require Tk 8.5\n"
        "ttk::button \".b\" -text \"Say Hello\" -command hello \n"
        "ttk::button \".c\" -text \"Run a script\" -command runscript\n"
        "pack \".b\" -padx 20 -pady 6\n"
        "pack \".c\" -padx 20 -pady 6\n"
        ;
    i.eval(script);
    Tk::runEventLoop();
}

祝你好运使用TCL和Tk!)

答案 2 :(得分:0)

Tcl::interpreter i(Tk::getInterpreter());

int main(int argc, char *argv[])
{

    i.def("runscript", runScript);
    std::string script = 
        "package require Tcl 8.5\n"
        "package require Tk 8.5\n"
        "ttk::button \".b\" -text \"Say Hello\" -command hello \n"
        "ttk::button \".c\" -text \"Run a script\" -command runscript\n"
        "pack \".b\" -padx 20 -pady 6\n"
        "pack \".c\" -padx 20 -pady 6\n"
        "after 10 vwait qwerasdfzxcv\n"
        ;
    i.eval(script);
    //Tk::runEventLoop(); //<== if you don't want stock here to make mainthread can keep running. you can add tcl script "after 10 vwait qwerasdfzxcv" as above showed.

}