在 Tkinter.Tcl() 中使用 Python 函数

发布于 2024-08-29 16:48:38 字数 1545 浏览 5 评论 0原文

  1. 我有很多 Python 函数。我们将它们称为 foobarbaz。它们接受可变数量的字符串参数并执行其他复杂的操作(例如访问网络)。

  2. 我希望“用户”(假设他只熟悉 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_isssetreinplace 等(Tcl 内置函数除外)被实现为 Python 函数。 ifforeachset 等是普通的 Tcl 结构。 post-configure 是一个 Python 函数,它接受稍后可以执行的 Tcl 代码块(这显然最终会调用上述 Python“函数”)。

这可以用 Python 实现吗?如果是这样,怎么办?

从 Tkinter 导入 *;根= Tk(); root.tk.eval('puts [array get tcl_platform]') 是我所知道的唯一集成,这显然非常有限(更不用说它在 mac 上启动 X11 服务器了)。

  1. I have a bunch of Python functions. Let's call them foo, bar and baz. They accept variable number of string arguments and does other sophisticated things (like accessing the network).

  2. I want the "user" (let's assume he is only familiar with Tcl) to write scripts in Tcl using those functions.

Here's an example (taken from Macports) that user can come up with:

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"
        }
    }
}

Here, variant_issset, reinplace are so on (other than Tcl builtins) are implemented as Python functions. if, foreach, set, etc.. are normal Tcl constructs. post-configure is a Python function that accepts, well, a Tcl code block that can later be executed (which in turns would obviously end up calling the above mentioned Python "functions").

Is this possible to do in Python? If so, how?

from Tkinter import *; root= Tk(); root.tk.eval('puts [array get tcl_platform]') is the only integration I know of, which is obviously very limited (not to mention the fact that it starts up X11 server on mac).

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

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

发布评论

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

评论(2

孤千羽 2024-09-05 16:48:38

经过一些实验,我发现你可以做这样的事情来创建一个 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

With a little experimentation I discovered you can do something like this to create a tcl interpreter, register a python command, and call it from 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

When I run the above code I get:

$ python2.5 /tmp/example.py
pycommand args: one, two, three
tcl result: None
2024-09-05 16:48:38

@Brian - 我必须进行实验才能获得正确的结果,

from Tkinter import Tcl
tcl = Tcl()
result = tcl.eval(' puts "hello, world" ')

请注意单引号和双引号的位置。这给了我预期的输出: hello, world

单引号或双引号的任何其他组合都会导致以下回溯:

  File "<stdin>", line 1, in <module>
_tkinter.TclError: can not find channel named "hello,"

--- fracjackmac

@Brian - I had to experiment in order to get the right result

from Tkinter import Tcl
tcl = Tcl()
result = tcl.eval(' puts "hello, world" ')

Note the placement of the single and double quotes. This gave me the expected output: hello, world

Any other combinations of single or double quotes resulted in the following traceback:

  File "<stdin>", line 1, in <module>
_tkinter.TclError: can not find channel named "hello,"

--- fracjackmac

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