0

I have a problem reading multiple lines of coordinates (like x,y) from a Tkinter Textbox. The user input will be this:

41,3
21,12
68,10
etc.

Each Line represents an x,y coordinate. X and Y are seperated by ,. I need to read this coordinates from the text box and process it in a way that an array forms. Like this:

[[41,3],[21,12],[68,10]

What i have so far:

from Tkinter import *


def get_Data():
   text_from_Box = Text_Entry.get("1.0", 'end-1c').split("\n")
   print text_from_Box


master = Tk()

Label(master, text = "Enter coordinates here:").grid(row = 0, sticky = W)

Text_Entry = Text(master, height = 30, width = 30)
Text_Entry.grid(row = 1, column = 0)

Button(master, text = 'Start Calculation', command = get_Data).grid(row = 2,      column = 0, sticky = W)

mainloop()

1 Answer 1

1

You have to split again at the ',' and convert to int (or float):

def get_Data():
   text_from_Box = Text_Entry.get("1.0", 'end-1c').split("\n")
   numbers = [[int(x) for x in pair.split(",")] for pair in text_from_Box]
   print numbers
Sign up to request clarification or add additional context in comments.

4 Comments

you can just do [map(float,i.split(",")) for i in text_from_box]
do you mean: 'numbers = [map(float,i.split(",")) for i in text_from_box]' that didn't work for me
@Eular Yes, but then I'd need to add a few pages of explanation why this works only in Python 2, but not in Python 3. ;-)
for python 3 wrap it up with a list()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.