[Tutor] Revised question-Make an object disappear

Peter Otten __peter__ at web.de
Tue Apr 26 11:39:34 EDT 2016


Lisa Hasler Waters wrote:

> Dear Tutors,
> 
> I have a student who is creating a game in TKinter. He wants the ball
> (object) to disappear when it hits when it hits the wall. He continues to
> get a syntax error message when trying to print the coordinates (he was
> printing the coordinates because he wanted to use its output to use again
> as inputs). He also wants to define the x-coordinates.

Having Python newbie questions channelled by someone who himself doesn't 
know the language makes meaningful communication a bit harder. Perhaps you 
can convince your student and/or yourself to let the student ask directly.

We bite, but are careful to avoid permanent marks...
 
> He's running OSX 10.5, Python 3.5.1.
> 
> I am pasting his code and the syntax error. Any assistance would be
> appreciated:
> 
>>>> print canvas.coords('dot1')

The above is Python 2.x syntax. In Python 3 print is a function -- you need 
parentheses around its arguments:

print(canvas.coord("dot1"))

>    if canvas.coords('dot1', x > 500, y > 500):
> NameError: name 'x' is not defined

Here x and y are part of expressions passed as arguments. As no variable 
called x exists Python fails at runtime. What you want is probably the 
bounding box which is returned by the coords method as a list of four 
floats.

dot_bbox = canvas.coords("dot1")

The basic way to extract values from a list is

x = dot_bbox[0] # left coord of the bounding box
y = dot_bbox[1] # top  coord ...

(You can extract right and bottom in the same way)

Now you have variables x and y and can perform tests like

if x > 500 or y > 500:
    ...  # whatever



More information about the Tutor mailing list