-
-
Notifications
You must be signed in to change notification settings - Fork 30.7k
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
Tkinter Canvas.coords does not flatten arguments #94473
Comments
Upon further inspection, it seems that canvas.coords do not allow doubly nested loops |
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. |
Because the numpy array is similar to a list, and lists works for Also doubly nested would be a list inside a list. So |
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()``` |
As mentioned on discuss.python.org the coords method requires at least 4 coordinates, not 2. The error message states this:
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. |
I see, thanks for the help. Just one more question before I close this issue, why does |
Good question. To be honest I'm not a Tkinter expert. Looking at the implementation and Tk documentation:
I don't know if that's intentional, but is does feel inconsistent. That said, If you don't mind I'll change to title of this issue to |
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. |
It would be a new feature. And I agree that it is worth to use _flatten on args. Tk canvas subcommands |
It is a pre-requisite for python#94473. Add tests for the coords() method and for creation of some Canvas items.
It is a prerequisite for python#94473. Add tests for the coords() method and for creation of some Canvas items.
It is a prerequisite for #94473. Add tests for the coords() method and for creation of some Canvas items.
It is a prerequisite for pythonGH-94473. Add tests for the coords() method and for creation of some Canvas items. (cherry picked from commit ff173ed) Co-authored-by: Serhiy Storchaka <[email protected]>
It is a prerequisite for pythonGH-94473. Add tests for the coords() method and for creation of some Canvas items. (cherry picked from commit ff173ed) Co-authored-by: Serhiy Storchaka <[email protected]>
It is a prerequisite for GH-94473. Add tests for the coords() method and for creation of some Canvas items. (cherry picked from commit ff173ed) Co-authored-by: Serhiy Storchaka <[email protected]>
It now accepts not only "x1, y1, x2, y2, ..." and "[x1, y1, x2, y2, ...]", but also "(x1, y1), (x2, y2), ..." and "[(x1, y1), (x2, y2), ...]".
Add more tkinter.Canvas tests (GH-98475) It is a prerequisite for GH-94473. Add tests for the coords() method and for creation of some Canvas items. (cherry picked from commit ff173ed) Co-authored-by: Serhiy Storchaka <[email protected]> Co-authored-by: Alex Waygood <[email protected]>
It now accepts not only "x1, y1, x2, y2, ..." and "[x1, y1, x2, y2, ...]", but also "(x1, y1), (x2, y2), ..." and "[(x1, y1), (x2, y2), ...]".
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)Your environment
Linked PRs
The text was updated successfully, but these errors were encountered: