[Tutor] lists and Entry

Emad Nawfal (عماد نوفل) emadnawfal at gmail.com
Wed Jan 14 12:43:38 CET 2009


On Wed, Jan 14, 2009 at 5:52 AM, Mr Gerard Kelly <s4027340 at student.uq.edu.au
> wrote:

> There is a little Tkinter program. It lets you type something in a box,
> and will display it at the command line.
>
>
> from Tkinter import *
>
> master = Tk()
>
> e = Entry(master)
> e.pack()
>
> e.focus_set()
>
> def callback():
>  s=e.get()
>  print s
>
> b = Button(master, text="get", width=10, command=callback)
> b.pack()
>
> mainloop()
>
>
>
> The get() method returns a string, how do I make it return a list?
> I want to be able to type in 1,2,3 into the box and get [1,2,3] to
> appear on the command line.
>
> If I change the callback() method so that it says
> s=[e.get()]
> I just get a list with one element, the string: ['1,2,3']
>
> If I make it
> s=list(e.get())
> I get a list with every character as an element: ['1', ',', '2', ',', '3']
>
> How to just get plain [1,2,3]?
>
> many thanks

One possibility is to split the string on the comma. If you have a string
like :
s = '1,2,3' then s.split(',') = [1,2,3]
If you later want to change this into a list of integers for example, you
might use something like  a list comprehension. Look at the following
interactive session:
>>> s = "1,2,3"

>>> m = s.split(',')
>>> m
['1', '2', '3']

>>> f = [int(i) for i in m]
>>> f
[1, 2, 3]
>>>


This is a reply from a programming amateur, so I think you might consider it
until you get a better solution.

>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.....محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"

Emad Soliman Nawfal
Indiana University, Bloomington
http://emnawfal.googlepages.com
--------------------------------------------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090114/63888ce8/attachment.htm>


More information about the Tutor mailing list