Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

itemconfigure returns incorrect text property of text items #44274

Closed
wmula mannequin opened this issue Nov 25, 2006 · 7 comments
Closed

itemconfigure returns incorrect text property of text items #44274

wmula mannequin opened this issue Nov 25, 2006 · 7 comments
Assignees
Labels
topic-tkinter type-bug An unexpected behavior, bug, or error

Comments

@wmula
Copy link
Mannequin

wmula mannequin commented Nov 25, 2006

BPO 1602742
Nosy @loewis, @mkiever, @serhiy-storchaka
Superseder
  • bpo-19020: Regression: Windows-tkinter-idle, unicode, and 0xxx filename
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/loewis'
    closed_at = <Date 2014-07-06.14:04:00.850>
    created_at = <Date 2006-11-25.16:27:13.000>
    labels = ['type-bug', 'expert-tkinter']
    title = 'itemconfigure returns incorrect text property of text items'
    updated_at = <Date 2014-07-06.14:04:00.849>
    user = 'https://bugs.python.org/wmula'

    bugs.python.org fields:

    activity = <Date 2014-07-06.14:04:00.849>
    actor = 'berker.peksag'
    assignee = 'loewis'
    closed = True
    closed_date = <Date 2014-07-06.14:04:00.850>
    closer = 'berker.peksag'
    components = ['Tkinter']
    creation = <Date 2006-11-25.16:27:13.000>
    creator = 'wmula'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 1602742
    keywords = []
    message_count = 7.0
    messages = ['61042', '61043', '64848', '73270', '73288', '197770', '222404']
    nosy_count = 6.0
    nosy_names = ['loewis', 'mkiever', 'wmula', 'gpolo', 'francismb', 'serhiy.storchaka']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '19020'
    type = 'behavior'
    url = 'https://bugs.python.org/issue1602742'
    versions = ['Python 2.7', 'Python 3.3', 'Python 3.4']

    @wmula
    Copy link
    Mannequin Author

    wmula mannequin commented Nov 25, 2006

    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 ''

    @wmula wmula mannequin assigned loewis Nov 25, 2006
    @wmula wmula mannequin added the topic-tkinter label Nov 25, 2006
    @wmula wmula mannequin assigned loewis Nov 25, 2006
    @wmula wmula mannequin added the topic-tkinter label Nov 25, 2006
    @mkiever
    Copy link
    Mannequin

    mkiever mannequin commented Jan 19, 2007

    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

    @mkiever
    Copy link
    Mannequin

    mkiever mannequin commented Apr 2, 2008

    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.

    @gpolo
    Copy link
    Mannequin

    gpolo mannequin commented Sep 15, 2008

    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).

    @gpolo
    Copy link
    Mannequin

    gpolo mannequin commented Sep 16, 2008

    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.

    @BreamoreBoy BreamoreBoy mannequin added type-bug An unexpected behavior, bug, or error labels Aug 25, 2010
    @serhiy-storchaka
    Copy link
    Member

    The patch for bpo-19020 fixes this issue.

    @francismb
    Copy link
    Mannequin

    francismb mannequin commented Jul 6, 2014

    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!

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    topic-tkinter type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants