This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: itemconfigure returns incorrect text property of text items
Type: behavior Stage: resolved
Components: Tkinter Versions: Python 3.3, Python 3.4, Python 2.7
process
Status: closed Resolution: duplicate
Dependencies: Superseder: Regression: Windows-tkinter-idle, unicode, and 0xxx filename
View: 19020
Assigned To: loewis Nosy List: francismb, gpolo, loewis, mkiever, serhiy.storchaka, wmula
Priority: normal Keywords:

Created on 2006-11-25 16:27 by wmula, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (7)
msg61042 - (view) Author: Wojciech Mula (wmula) Date: 2006-11-25 16:27
Tkinter: canvas itemconfigure bug

Consider following code:

-- tkbug.py ---
from Tkinter import *
root   = Tk()
canvas = Canvas(root)
text   = "sample text with spaces"
id     = canvas.create_text(0, 0, text=text)

text2  = canvas.itemconfigure(id)['text'][-1]

print text
print text2
--- eof ---

This toy prints:

sample text with spaces
('sample', 'text', 'with', 'spaces')

The returned value is not a string -- Tk returns the same
string as passed on creating item, but Tkinter split it.
To fix this problem, internal method '_configure' have
to be changed a bit:

*** Tkinter.py.old	2006-11-20 16:48:27.000000000 +0100
--- Tkinter.py	2006-11-20 17:00:13.000000000 +0100
***************
*** 1122,1129 ****
              cnf = _cnfmerge(cnf)
          if cnf is None:
              cnf = {}
!             for x in self.tk.split(
                      self.tk.call(_flatten((self._w, cmd)))):
                  cnf[x[0][1:]] = (x[0][1:],) + x[1:]
              return cnf
          if type(cnf) is StringType:
--- 1122,1134 ----
              cnf = _cnfmerge(cnf)
          if cnf is None:
              cnf = {}
!             for x in self.tk.splitlist(
                      self.tk.call(_flatten((self._w, cmd)))):
+                 if type(x) is StringType:
+                     if x.startswith('-text '):
+                         x = self.tk.splitlist(x)
+                     else:
+                         x = self.tk.split(x)
                  cnf[x[0][1:]] = (x[0][1:],) + x[1:]
              return cnf
          if type(cnf) is StringType:


Maybe better/faster way is to provide Canvas method, that
return a 'text' property for text items:

---
def get_text(self, text_id):
	try:
		r = self.tk.call(self._w, 'itemconfigure', text_id, '-text')
		return self.tk.splitlist(r)[-1]
	except TclError:
		return ''
---
msg61043 - (view) Author: Matthias Kievernagel (mkiever) * Date: 2007-01-19 18:35
There is a simple workaround: use itemcget.

The error applies to other options as well:
  dash, activedash, disableddash, tags, arrowshape, font
These options also may contain a space in their value.
I collected this information from 'man n Canvas' from Tk 8.4.6
I hope I didn't miss any.

BTW the itemconfigure document string is broken.

Greetings,
Matthias Kievernagel
msg64848 - (view) Author: Matthias Kievernagel (mkiever) * Date: 2008-04-02 11:11
I no longer know what I meant with "document string is 
broken".
It is not very clear, but not broken.
It does not specify clearly the return value
in the case of 'without arguments'.

The problem is still present in trunk.
Problem is as far as I understand it:
Some return values from tcl are strings
that may contain spaces (or are even certain to contain 
spaces).
They are nonetheless translated to a Python tuple by
Tcl_SplitList in _tkinter.c.
There is no difference in syntax between tcl lists
and tcl strings (with spaces).
You have to have contextual knowledge to do the right thing.
This knowledge cannot be attached to itemconfigure alone
(for reconstitution of strings) because then information
about whitespace is already lost.
For return values known to be strings _tkinter/TkApp_SplitList
must not be used.

So I think it's a more general bug related to tcl string
return values.
Other Tkinter bugs may have the same explanation.
Don't know how to resolve this without a lot of work.

Matthias.
msg73270 - (view) Author: Guilherme Polo (gpolo) * (Python committer) Date: 2008-09-15 17:07
The problem is actually on Tkinter side, not really tcl/tk fault here.
Tkinter should be formatting that text option as "{text here}" when the
value contains one or more spaces (it is actually fine to use this tcl
formatting when there are no spaces either).

To try this yourself, just change text to:
text = "{sample text with spaces}"

I can't look at Tkinter source right now to propose a correct solution,
but will do later (today hopefully).
msg73288 - (view) Author: Guilherme Polo (gpolo) * (Python committer) Date: 2008-09-16 01:37
Uhm, now I see.. Tkinter already formats it correctly, and you shouldn't
be using itemconfigure for this task. If you try it directly in tk, like
this:

canvas .c
.c create text 0 0 -text {a b}
.c itemconfigure 1 -text

You would get something like this:

-text {} {} {} {a b}

While

.c itemcget 1 -text

Will return the same as Python: "a b"

Now what remains is to see how useful is to use itemconfigure for this,
and if it is worth making canvas.itemconfigure(id)['text'][-1] return "a
b" instead of ("a", "b"). Changing Misc._configure is too risky given
there are no tests for Tkinter (and I find it weird sometimes, someone
will still have to explain me why Tkinter plays with cnf and kw all the
time), the other option involves leaving these special needings in Text
and is something I dislike because other widgets could use these new
things that would be added. 
Yet another option would be to start writing unit tests for Tkinter and
much of these bugs would end up being caught and hopefully fixed properly.
msg197770 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-09-15 13:02
The patch for issue19020 fixes this issue.
msg222404 - (view) Author: Francis MB (francismb) * Date: 2014-07-06 13:30
Hi,
just a question:

the status of this issue is pending but it seems to be already
resolved/duplicated. Means that this issue can be closed?

Thansk in advance!
History
Date User Action Args
2022-04-11 14:56:21adminsetgithub: 44274
2014-07-06 14:04:00berker.peksagsetstatus: open -> closed
2014-07-06 13:30:17francismbsetstatus: pending -> open
nosy: + francismb
messages: + msg222404

2014-02-09 19:35:03serhiy.storchakasetstatus: open -> pending
superseder: Regression: Windows-tkinter-idle, unicode, and 0xxx filename
resolution: duplicate
stage: needs patch -> resolved
2013-09-15 13:02:03serhiy.storchakasetnosy: + serhiy.storchaka

messages: + msg197770
versions: + Python 3.3, Python 3.4, - Python 3.1, Python 3.2
2010-08-25 14:25:04BreamoreBoysetstage: needs patch
type: behavior
versions: + Python 3.1, Python 2.7, Python 3.2, - Python 2.6, Python 2.4
2008-09-16 01:37:18gpolosetresolution: accepted -> (no value)
messages: + msg73288
2008-09-15 17:07:23gpolosetnosy: + gpolo
resolution: accepted
messages: + msg73270
2008-04-02 11:11:14mkieversetmessages: + msg64848
versions: + Python 2.6
2006-11-25 16:27:13wmulacreate