如何在同一个程序中同时使用C++TCL和C++TK?
因此,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 UI 回调来做复杂的事情,包括运行其他 Tcl 代码:
您还需要查看 Tcl/Tk 事件循环支持的其他回调,包括计时器和文件 I/O。
You can use UI callbacks to do complicated things, including running other Tcl code:
You'll also want to look at other callbacks, including timers and file I/O, that the Tcl/Tk event loop supports.
所以我们已经做到了...)这是我们的 svn,已更新,可以正常工作C++/Tk 和 C++/TCL 进行了修补以相互配合,并纠正了官方 sourceforge 网站上的一些错误。
这是受启发的代码示例 作者:Ben:
祝您使用 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:
Have a good luck in using TCL and Tk!)
}
}