Does Tkinter provide any way of properly converting a Python list to a Tcl list? Specifically, I am trying to insert text into a Text widget with multiple tags, which is supposedly allowed by passing a list of tag names. However, passing a Python list doesn't work - as far as I can tell, the list just gets converted with str() and then split on whitespace! The results (as verified with .tag_names()) is a mishmash of word fragments, including commas, quote marks, and brackets that could have only come from the stringification of the list: no such characters were actually part of the tags in the list. Jason Harper
Python tuples, including nested tuples, are converted to Tcl lists. Lists are just converted to strings.
import Tkinter tk = Tkinter.Tk().tk tk.call("list", [1, "a b", 3, "{"]) # A list is just converted "\\[1,\\ 'a\\ b',\\ 3,\\ '\\{'\\]" # as by str() tk.call("list", (1, "a b", 3, "{")) # A tuple is converted '{1 {a b} 3 \\{}' # to a tcl list tk.call("list", (1, (2, 3, "a b"), "c d")) # Nested example '{1 {2 3 {a b}} {c d}}' tk.call("list", 1, "a b", 3, "{") # Positional args are quoted '1 {a b} 3 \\{' # as needed
So where you're trying to do text.insert(0.0, "blah", [other, args, here]) you should try text.insert(0.0, "blah", tuple([other, args, here])) or text.insert(0.0, "blah", *[other, args, here]) or text.insert(0.0, "blah", other, args, here) I just did the following and got "what I expected":
import Tkinter t = Tkinter.Text() t.tag_configure("red", background="red") t.tag_configure("blue", background="blue") t.pack() t.insert(0.0, "blah", "red", " ", "", "bleep", "blue")
Jeff
participants (2)
-
Jason Harper -
Jeff Epler