问题
how to pass more than one arguments to tcl proc using Tkinter. i want to pass 3 args to tcl proc tableParser from python proc validate_op which has 3 args...
from Tkinter import Tcl
import os
tcl = Tcl()
def validate_op(fetch, header, value):
tcl.eval('source tcl_proc.tcl')
tcl.eval('set op [tableParser $fetch $header $value]') <<<<< not working
proc tableParser { result_col args} {
..
..
..
}
回答1:
The simplest way to handle this is to use the _stringify function in the Tkinter module.
def validate_op(fetch, header, value):
tcl.eval('source tcl_proc.tcl')
f = tkinter._stringify(fetch)
h = tkinter._stringify(header)
v = tkinter._stringify(value)
tcl.eval('set op [tableParser %(f)s %(h)s %(v)s]' % locals())
These two questions, while not answering your question, were useful in answering it:
- Pass Python variables to `Tkinter.Tcl().eval()`
- Is there a Python equivalent to Ruby's string interpolation?
回答2:
If you do not insist on eval, you can also do it like this:
def validate_op(fetch, header, value):
tcl.eval('source tcl_proc.tcl')
# create a Tcl string variable op to hold the result
op = tkinter.StringVar(name='op')
# call the Tcl code and store the result in the string var
op.set(tcl.call('tableParser', fetch, header, value))
If your tableParser returns a handle to some expensive to serialize object, this is probably not a good idea, as it involves a conversion to string, which is avoided in the eval case. But if you just need to get a string back, this is just fine and you do not need to deal with the _stringify function mentioned in Donals answer.
来源:https://stackoverflow.com/questions/37875115/python-pass-more-than-one-arguments-to-tcl-proc-using-tkinter-tcl-eval