Using Python functions in Tkinter.Tcl()

后端 未结 2 959
南方客
南方客 2021-01-03 09:55
  1. I have a bunch of Python functions. Let\'s call them foo, bar and baz. They accept variable number of string arguments and do

2条回答
  •  青春惊慌失措
    2021-01-03 10:17

    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
    

提交回复
热议问题