GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Tkinter Canvas.coords does not flatten arguments · Issue #94473 · python/cpython

Tkinter Canvas.coords does not flatten arguments #94473

New issue
Copy link
New issue
Copy link
Closed
Closed
Tkinter Canvas.coords does not flatten arguments#94473
Copy link
Labels
topic-tkintertype-featureA feature request or enhancementA feature request or enhancement

Description

@Why-not-now
Why-not-now
opened on Jul 1, 2022
Issue body actions

Bug report
Double nested arrays in tk.coords will produce errors in tkinter (_tkinter.TclError: wrong # coordinates: expected at least 4, got 2 in this case)

import tkinter as tk

coords = [[100, 100], [300, 300]]


root = tk.Tk()
canvas = tk.Canvas(width=400,
                   height=400,
                   background="bisque")
canvas.pack(fill="both", expand=True)
line = canvas.create_line(coords)
coords[1] = [200, 200]
canvas.coords(line, coords)  # line with error

root.mainloop()

Your environment

  • CPython versions tested on: 3.10.4
  • Operating system: Windows 10

Linked PRs

  • gh-94473: Flatten arguments in tkinter.Canvas.coords() #98479

Activity

  1. Why-not-now
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Jul 1, 2022
  2. Why-not-now commented on Jul 1, 2022

    @Why-not-now
    Why-not-now
    on Jul 1, 2022
    Author
    More actions

    Upon further inspection, it seems that canvas.coords do not allow doubly nested loops

  3. stevendaprano commented on Jul 1, 2022

    @stevendaprano
    stevendaprano
    on Jul 1, 2022
    Member
    More actions

    Is there something that suggests to you that using a numpy array for coords ought to work?

    I don't know what you mean by double nested loops. There's no loop here.

  4. Why-not-now commented on Jul 1, 2022

    @Why-not-now
    Why-not-now
    on Jul 1, 2022
    Author
    More actions

    Because the numpy array is similar to a list, and lists works for canvas.create_line() etc.

    Also doubly nested would be a list inside a list. So canvas.coords(line, [[100, 100], [300, 300]]) wouldn't work while canvas.create_line([[100, 100], [300, 300]]) would work

  5. Why-not-now commented on Jul 1, 2022

    @Why-not-now
    Why-not-now
    on Jul 1, 2022
    Author
    More actions

    Example of this is

    import numpy as np
    import tkinter as tk
    
    
    root = tk.Tk()
    canvas = tk.Canvas(width=400,
                       height=400,
                       background="bisque")
    canvas.pack(fill="both", expand=True)
    line = canvas.create_line([[100, 100], [300, 300]])
    canvas.coords(line, [[100, 100], [200, 200]])  # line with error
    
    root.mainloop()```
  6. ronaldoussoren commented on Jul 1, 2022

    @ronaldoussoren
    ronaldoussoren
    on Jul 1, 2022
    Contributor
    More actions

    As mentioned on discuss.python.org the coords method requires at least 4 coordinates, not 2. The error message states this:

    _tkinter.TclError: wrong # coordinates: expected at least 4, got 2
    

    Supporting numpy arrays for coordinates, or rather anything other than builtin lists and tuples, could be a feature request, but would require a clear use case that sells this change.

  7. Why-not-now commented on Jul 1, 2022

    @Why-not-now
    Why-not-now
    on Jul 1, 2022
    Author
    More actions

    I see, thanks for the help. Just one more question before I close this issue, why does canvas.coords(line, [[100, 100], [200, 200]]) not represent 4 coordinates while canvas.create_line([[100, 100], [300, 300]]) does?

  8. ronaldoussoren commented on Jul 2, 2022

    @ronaldoussoren
    ronaldoussoren
    on Jul 2, 2022
    Contributor
    More actions

    Good question. To be honest I'm not a Tkinter expert.

    Looking at the implementation and Tk documentation:

    • The Tk implementation represents the various x, y coordinates as separate arguments (see https://www.tcl.tk/man/tcl8.0/TkCmd/canvas.html#M40)
    • The Python wrapper for the create_* methods flattens arguments, basically turning canvas.create_line((startX, startY), (stopX, stopY)) into canvas.create_line(startX, startY, stopX, stopY)
    • The Python wrapper for coord method does not flatten, which means you have to pass the various coordinates as separate arguments (canvas.coords(line, startX, startY, stopX, stopY))

    I don't know if that's intentional, but is does feel inconsistent. That said, canvas.coords(line) will return the current coordinates of the line (or other object) as an flattened list as well. Changing that is not backward compatible and IMHO is not worth changing even if we'd add argument flattening to the coords wrapper.

    If you don't mind I'll change to title of this issue to Tkinter Canvas.coords does not flatten arguments, and leave it open for someone that knows more about Tk to comment on whether or not this is intentional behaviour.

  9. AlexWaygood
    added
    topic-tkinter
    on Jul 2, 2022
  10. Why-not-now
    changed the title [-]Numpy array not compatible with tkinter[/-] [+]Inconsistent results for canvas.coords and canvas.create_<widget>[/+] on Jul 2, 2022
  11. Why-not-now
    changed the title [-]Inconsistent results for canvas.coords and canvas.create_<widget>[/-] [+]Tkinter Canvas.coords does not flatten arguments[/+] on Jul 2, 2022
  12. Why-not-now commented on Jul 3, 2022

    @Why-not-now
    Why-not-now
    on Jul 3, 2022
    Author
    More actions

    Looking through the source code, at line 2817 of tkinter/__init__.py,

        def coords(self, *args):
            """Return a list of coordinates for the item given in ARGS."""
            # XXX Should use _flatten on args
            return [self.tk.getdouble(x) for x in
                               self.tk.splitlist(
                       self.tk.call((self._w, 'coords') + args))]

    there is one comment saying they should use _flatten on args.

    Reacted by Akuli, Benedek Dévényi, Serhiy Storchaka and Why-not-now
  13. serhiy-storchaka
    added
    type-featureA feature request or enhancementA feature request or enhancement
    and removed
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Oct 14, 2022
  14. serhiy-storchaka commented on Oct 14, 2022

    @serhiy-storchaka
    serhiy-storchaka
    on Oct 14, 2022
    Member
    More actions

    It would be a new feature. And I agree that it is worth to use _flatten on args. Tk canvas subcommands coord and create has the same signature for coordinates. It natively supports both canvas.coords(line, 100, 100, 200, 200) and canvas.coords(line, [100, 100, 200, 200]), but unfortunately not more natural in Python canvas.coords(line, [100, 100], [200, 200]) and canvas.coords(line, [[100, 100], [200, 200]]).

  15. serhiy-storchaka
    added 3 commits that reference this issue on Oct 20, 2022

    Add more tkinter.Canvas tests

    c009190

    Add more tkinter.Canvas tests

    a7dfcce

    Add more tkinter.Canvas tests (GH-98475)

    ff173ed
  16. miss-islington
    added 3 commits that reference this issue on Oct 20, 2022

    Add more tkinter.Canvas tests (pythonGH-98475)

    f740463

    Add more tkinter.Canvas tests (pythonGH-98475)

    c469ec2

    Add more tkinter.Canvas tests (GH-98475)

    05111d0
  17. serhiy-storchaka
    added a commit that references this issue on Oct 20, 2022

    pythongh-94473: Flatten arguments in tkinter.Canvas.coords()

    4e6c232
  18. ambv
    added a commit that references this issue on Oct 28, 2022

    [3.10] Add more tkinter.Canvas tests (GH-98475) (#98477)

    97106d3
  19. serhiy-storchaka
    added a commit that references this issue on May 22, 2023

    gh-94473: Flatten arguments in tkinter.Canvas.coords() (GH-98479)

    9bc80da
  20. serhiy-storchaka
    closed this as completedon May 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic-tkintertype-featureA feature request or enhancementA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.