python tcl 控件_在Tkinter.Tcl()中使用Python函数
>我有一堆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
標簽:python,dsl,tcl,integration
來源: https://codeday.me/bug/20190827/1740920.html
總結
以上是生活随笔為你收集整理的python tcl 控件_在Tkinter.Tcl()中使用Python函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php mysql db封装类_封装自己
- 下一篇: php 中文相等_php判断字符串是否相