在Tkinter.Tcl()中使用Python函数
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了在Tkinter.Tcl()中使用Python函数,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1808字,纯文字阅读大概需要3分钟。
内容图文
![在Tkinter.Tcl()中使用Python函数](/upload/InfoBanner/zyjiaocheng/729/d32af307ceb7479089876a3db2553565.jpg)
>我有一堆Python函数.我们称他们为foo,bar和baz.它们接受可变数量的字符串参数,并执行其他复杂的操作(如访问网络).
>我希望“用户”(让我们假设他只熟悉Tcl)使用这些函数在Tcl中编写脚本.
以下是用户可以提出的示例(取自Macports):
post-configure {
if {[variant_isset universal]} {
set conflags ""
foreach arch ${configure.universal_archs} {
if {${arch} == "i386"} {append conflags "x86 "} else {
if {${arch} == "ppc64"} {append conflags "ppc_64 "} else {
append conflags ${arch} " "
}
}
}
set profiles [exec find ${worksrcpath} -name "*.pro"]
foreach profile ${profiles} {
reinplace -E "s|^(CONFIG\[ \\t].*)|\\1 ${conflags}|" ${profile}
# Cures an isolated case
system "cd ${worksrcpath}/designer && ${qt_dir}/bin/qmake -spec ${qt_dir}/mkspecs/macx-g++ -macx -o Makefile python.pro"
}
}
}
这里,variant_issset,reinplace等(除了Tcl builtins)实现为Python函数. if,foreach,set等等是正常的Tcl结构. post-configure是一个Python函数,它接受一个Tcl代码块,以后可以执行(反过来显然最终会调用上面提到的Python“函数”).
这可以用Python做吗?如果是这样,怎么样?
来自Tkinter进口*; root = Tk(); root.tk.eval(‘puts [array get tcl_platform]’)是我所知道的唯一集成,显然非常有限(更不用说它在mac上启动X11服务器的事实).
解决方法:
通过一些实验,我发现你可以做这样的事情来创建一个tcl解释器,注册一个python命令,并从Tcl调用它:
import Tkinter
# create the tcl interpreter
tcl = Tkinter.Tcl()
# define a python function
def pycommand(*args):
print "pycommand args:", ", ".join(args)
# register it as a tcl command:
tcl_command_name = "pycommand"
python_function = pycommand
cmd = tcl.createcommand(tcl_command_name, python_function)
# call it, and print the results:
result = tcl.eval("pycommand one two three")
print "tcl result:", result
当我运行上面的代码时,我得到:
$python2.5 /tmp/example.py
pycommand args: one, two, three
tcl result: None
内容总结
以上是互联网集市为您收集整理的在Tkinter.Tcl()中使用Python函数全部内容,希望文章能够帮你解决在Tkinter.Tcl()中使用Python函数所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。