简体   繁体   中英

Python Tkinter multiple line input as integer in list

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()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM