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

发布于 2024-11-18 17:23:22 字数 2003 浏览 1 评论 0原文

因此,C++/TCL 为我们提供了通过 API 轻松管理的 TCL C++ 函数和类,这非常棒:

#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 中处理系统事件循环非常好正如

#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();
}

您所看到的,一个非常适合 API 创建,另一个非常适合 GUI 渲染。

我想找到一种方法将它们混合起来,例如这样的代码可以工作:

#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?

更新:

所以我们已经做到了。需要修复一些 CPP/TCL 和 CPP/Tk 源代码,请参阅我们的 svn< /a> 和 我的回答例如使用

So it is great of C++/TCL to provide us with easy manageable in TCL C++ functions and classes via APIs like:

#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);
}

At he same time its great to have an system event loop handled in C++/Tk with api's like

#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();
}

So as you can see one is great for API creation another for GUI rendering.

I want to find a way to mix them to have for example such code working:

#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();
}

How to do such thing possible? How to mix C++/TCL and C++/Tk?

Updete:

And so we have done it. Required some CPP/TCL and CPP/Tk sorurce code fixing, see our svn, and my answer for example of use.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鹿! 2024-11-25 17:23:22

您可以使用 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。

You can use UI callbacks to do complicated things, including running other Tcl code:

#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();
}

You'll also want to look at other callbacks, including timers and file I/O, that the Tcl/Tk event loop supports.

沉溺在你眼里的海 2024-11-25 17:23:22

所以我们已经做到了...)这是我们的 svn,已更新,可以正常工作C++/Tk 和 C++/TCL 进行了修补以相互配合,并纠正了官方 sourceforge 网站上的一些错误。

这是受启发的代码示例 作者: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 一切顺利!)

So we have done it...) here is our svn with updated, working C++/Tk and C++/TCL, patched for work with each other, corrected from some errors that were on official sourceforge site.

And here is code sample inspired 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();
}

Have a good luck in using TCL and Tk!)

醉生梦死 2024-11-25 17:23:22
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.

}

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.

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文