python tkinter canvas create_line function returns values?

Viewed 305

i have recently encountered some return values in python interactive mode(i never get create_line function's return values in script mode and in case testing the lines individual in interactive mode i get return values but the function does not create lines like i expected). I was wondering what these meant and why is the line not getting created. Here are the few I have encountered till now:

>>> canvas.create_line(134,100,134,400)
1
>>> 

and i got these return values too

>>> def shape():
    canvas.create_line(134,100,134,400)
    canvas.create_line(134,400,234,400)
    canvas.create_line(234,100,234,400)
    canvas.create_line(234,100,134,100)

    
>>> shape()
>>> canvas.create_line(134,100,134,400)
5
>>> canvas.create_line(134,400,234,400)
6
>>> canvas.create_line(234,100,234,400)
7
>>> canvas.create_line(234,100,134,100)
8
>>> 

in this code, when i called shape function then it did not create the lines. Calling these lines individually gave me these return values, the odd this was how the values started from 5 and then went till 8 consecutively. Can someone please tell if this is a bug or do these numbers have a meaning?

1 Answers

When you say canvas.create_line() it returns an Id of the canvas object created, that later can be used to modify or get properties and so on.

Here is something about create_arc() as I could not find anything official about create_line():

The constructor returns the object ID of the new arc object on canvas.

From your example, seems like only create_line() and create_image() returns integer id, the rest gives an object id.

So for example:

obj = canvas.create_line(134,100,134,400)
canvas.itemconfig(obj, width=10)

Canvas methods take tagorId, so you can always just use an option tag='line1' and then call on it later, like:

obj = canvas.create_line(134,100,134,400,tag='line1')
canvas.itemconfig('line1', width=10)

in this code, when I called shape function then it did not create the lines.

Are you sure the canvas coordinates are correct? It starts from 0,0 of the canvas, try changing the coordinates and configure what works for your canvas.

Related