3

I am new to this forum, so I apologize beforehand for any mistakes. I am trying to pass a variable from my python code to tcl script. The way I am doing this is:

import os
import Tkinter
from Tkinter import *
def set_impairments(port,delay):
 port=int(port)
 delay=int(delay)
 tcl = Tcl()
 tcl.eval("""
 proc my_proc {in_port,in_delay} {
 puts in_port
 puts in_delay
 }
 """)
 print port
 print delay
 tcl.eval('my_proc port,delay')
set_impairments(0,25000)

The output of this code is:

0
25000
in_port
in_delay

How to assign in_port to 0 and in_delay to 25000? I am not able to understand why it is not able to pass the value of the variable.

femtoRgon
33.4k7 gold badges67 silver badges90 bronze badges
asked Dec 22, 2015 at 19:31
2
  • This is partially a duplicate of stackoverflow.com/questions/25288972/… but you can also use the 'eval' + format solution below instead of using a Tkinter StringVar. Commented Dec 22, 2015 at 20:24
  • Yeah I have gone through that thread and it didn't help me. Apparently I was missing the point as Glenn suggested to put a $ to reference the value of the variable. Anyway Thanks. Commented Dec 23, 2015 at 16:46

1 Answer 1

3

Tcl doesn't use commas to separate arguments, it uses whitespace. Also, you need a $ to reference the value of the variable. Do this:

proc my_proc {in_port in_delay} {
 puts $in_port
 puts $in_delay
}

I'm not really a python guy, but you probably want

tcl.eval('my_proc {0} {1}'.format(port,delay))
answered Dec 22, 2015 at 19:48
Sign up to request clarification or add additional context in comments.

3 Comments

One can write the call a bit nicer as tcl.call('my_proc', port, delay) which has a bit of magic so you do not need to move through the string rep. Does not work for all python variable types though, but int / string works.
Thank you so much Glenn.This helped.
Thank You Schlenk. I will definitely look at it.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.