Reputation: 926
I have a program that creates a small GUI for a function. In particular, I have something like this:
def create_widgets(self):
self.entryLabel = Label(self, text="Please enter a list of numbers:")
self.entryLabel.grid(row=0, column=0, columnspan=2)
self.listEntry = Entry(self)
self.listEntry.grid(row=0, column=2, sticky=E)
self.entryLabel = Label(self, text="Please enter an index value:")
self.entryLabel.grid(row=1, column=0, columnspan=2, sticky=E)
self.indexEntry = Entry(self)
self.indexEntry.grid(row=1, column=2)
self.runBttn = Button(self, text="Run Function", command=self.Function)
self.runBttn.grid(row=2, column=0, sticky=W)
self.answerLabel = Label(self, text="Output List:")
self.answerLabel.grid(row=2, column=1, sticky=W)
Then for the definition of the function I have a part afterwards:
self.answer = Label(self, text=rtn)
self.answer.grid(row=2, column=2, sticky=W)
that tells it what to give as output.
Now, the issue is the input is entered as a string (say 12345) and so it would recognize the numbers 10,11,12 and so on as their individual digits. Is there a simple way to modify this to require list inputs to be comma delimited (e.g. 1,2,3,4,5)?
Thanks in advance.
Upvotes: 0
Views: 66
Reputation: 76194
If you are just asking how to turn "1,2,3,4,5"
into a list of integers, you can do it with split
and int
.
user_input = "4,8,15,16,23,42"
numbers = [int(d) for d in user_input.split(",")]
print "numbers:", numbers
print "sum of numbers:", sum(numbers)
Result:
numbers: [4, 8, 15, 16, 23, 42]
sum of numbers: 108
Upvotes: 1