0

I made a list in python named "worldPos" and it constantly adds the position of the mouse cursor to the list as x and y coordinates. How can I get the most recent x and y coordinates added to the list?

from Tkinter import *

root = Tk()
root.geometry('800x800')

cnv = Canvas(root, width = 800, height = 800)
cnv.pack()
worldPos = []

def motion(event):
    x, y = event.x, event.y

    worldPos.append((x,y))

root.bind('<Motion>', motion)

mainloop()

1 Answers1

2

The .append() method adds to the very end of the list.

This location can be addressed by list_name[-1]. Where the minus sign indicates indexing from the back of the list.

However you are adding two items as a tuple, so you'll want to pull:

x,y = worldPos[-1]

If you instead added the items to the front of the list

worldPos[0:0] = (x,y)

You would then index from the front of the list

x,y = worldPos[0]

Enjoy and good luck!

p.s. Be sure to accept this as an answer if it answers your question.

Alea Kootz
  • 913
  • 4
  • 11