1

I have a small application that takes in a string of numbers, runs them through a function, and spits out the output. For the list entry I have the following set up:

 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)

However, this only allows me to input strings (e.g. 123451011) while I would like it to be able to recognize individual numbers (e.g. 1,2,3,4,5,10,11). I suppose essentially what I'm asking is to be able to use a list instead of a string. Is there a way to change self.listEntry to handle this? Is there something I can add into my function instead (which currently inputs valueList = list(self.listEntry.get()))? Thanks!

EDIT:

I defined a function as follows:

    def Function(self):
        valueList = list([int(x) for x in self.listEntry.get().split(",")])
        x = map(int, valueList)

Which then continues to outline how to run the numbers and tells the program to give the output.

2 Answers 2

2

You can do something like this:

import Tkinter as tk

root = tk.Tk()

entry = tk.Entry()
entry.grid()

# Make a function to go and grab the text from the entry and split it by commas
def get():
    '''Go and get the text from the entry'''

    print entry.get().split(",")

# Hook the function up to a button
tk.Button(text="Get numbers", command=get).grid()

root.mainloop()

Or, if you want them all to be integers, change this line:

print entry.get().split(",")

to this:

print map(int, entry.get().split(","))
Sign up to request clarification or add additional context in comments.

4 Comments

I'm not entirely sure I follow. Is the idea to create a separate button to get numbers in the right format? Thanks for your help!
@Stopwatch - Yes. Make a button that is hooked up to a function. The function should grab the text from the entry and then split it by commas. The result returned by this function will be a list containing what you want.
Thanks iCodez, both yours and Bryan's answers were very helpful.
@Stopwatch: you don't need a button per se, but obviously there must be some code somewhere that is using the data from the entry widget. Wherever that code is -- whether called from a button or not -- you need to convert the data to whatever format you need. The button in this code is just a convenient way to give a short example.
0

You can just use split on the input, and then map int to the list that that generates.

self.listEntry = [int(x) for x in Entry(self).split(',')]

11 Comments

This won't work at all because Entry doesn't have a split method.
@user1612868 Thanks. So should the edited version look something like the above edit? Edit: Nevermind, based on what iCodez has just said!
Your description is correct, but the code is all wrong. To get the values as a list of integers you would do [int(x) for x in self.listEntry.get().split(",")]. What your code does is create a new entry widget, then splits the entry widget name (which is what the string representation of an entry widget object is).
@BryanOakley Where would I place that sort of thing? Would it be in place of self.listEntry = Entry(self)?
@Stopwatch: there is nothing in your code to prevent the user from entering 1,2,3,4,5. Have you actually tried to run your own program?
|

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.