-1

I wish to have each item in my list printed on a different line in the same textbox in EasyGUI. How do I code for this?

EDIT: Sorry, I was a bit vague. Let me explain further. I have a list called combinations. It has 50-100 entries. When I try to output it in a textbox like this:

eg.textbox(combinations)

It prints it in a sort of paragraph format.Like this:

item, item, item, item, item, item, etc.

I want to print it more like a list, with one item per line, like this:

item,

item,

item,

item,

How do I code for this?

Hersh S.
  • 197
  • 2
  • 10
  • What do you have so far? – George Apr 08 '12 at 01:24
  • @George Well, I have a list called combinations. It has 50-100 entries. When I try to print the list(in a textbox), it prints it in one huge blob of text, in a textbox. What I want is to have each item in the list on separate lines. I'm stumped on this one, I got nothing. – Hersh S. Apr 08 '12 at 01:32
  • @HershS.: He means that you should update your question with the description you gave, _and_ you should post the relevant code as well. – Joel Cornett Apr 08 '12 at 01:33
  • @HershS. did you try appending newlines to the items in your list? – Joel Cornett Apr 08 '12 at 01:40
  • @Joel That might work...How do I do that? – Hersh S. Apr 08 '12 at 01:46
  • you can do `for item in combinations:`, that will print them each on their separate line. I don't know if this is what you are talking about http://codepad.org/OqCiMAWI – George Apr 08 '12 at 02:44
  • @HershS. : Append "\n" to the end of each string in combinations. Not sure if that will work, but it's worth a try. Maybe something like `combinations = [item + "\n" for item in combinations]`. I'm assuming that `combinations` is a list of strings. – Joel Cornett Apr 08 '12 at 12:15
  • @JoelCornett, I'm sorry I didn't clarify this earlier, it's a two-dimensional list. But won't appending /n after every sub-list still work? – Hersh S. Apr 08 '12 at 18:59
  • @HershS.: Yes, something like `[repr(item) + "\n" for item in twoDList]` should work. – Joel Cornett Apr 08 '12 at 20:14
  • possible duplicate of [EasyGUI Output?](http://stackoverflow.com/questions/10058621/easygui-output) – joaquin Apr 08 '12 at 22:14
  • @JoelCornett: here's and example of an entry in the list: [[["Ww"],["Aa"],["Bb"],["Cc"]]]. Given that, the function appears to do something, however, when I output it via a textbox, the textbox is blank. – Hersh S. Apr 09 '12 at 19:19
  • @HershS.: I just posted a possible solution. Let me know if it works. – Joel Cornett Apr 09 '12 at 19:29

1 Answers1

1

I think your problem may be that your list is to deep. According to the documentation, the textbox takes as input either a string or a list of strings. You are passing it a list of lists of strings. If you flatten out the data, it may work.

Try this:

>>>l = [[["Ww"],["Aa"],["Bb"],["Cc"]]]
>>>
>>> def flatten(sList,eList = None):
...     if not eList: eList = []
...     for item in sList:
...             if type(item) == list:
...                     eList = flatten(item, eList)
...             else:
...                     eList.append(item)
...     return eList
... 
>>> flatten(l)
['Ww', 'Aa', 'Bb', 'Cc']
>>> 

UPDATE

I downloaded EasyGUI and confirmed that this is in fact, your problem. All that you need to do now is append a newline to each item in your flattened list.

>>>flat_list = ['Ww', 'Aa', 'Bb', 'Cc']
>>>with_newlines = '\n'.join(flat_list)
>>>with_newlines
'Ww\nAa\nBb\nCc\n'
>>>eg.textbox(with_newlines)
Joel Cornett
  • 24,192
  • 9
  • 66
  • 88
  • 1
    -1 This is making easy things complicated. Just do: `'\n'.join(flat_list)`. [Check this](http://stackoverflow.com/questions/10058621/easygui-output/10058987#comment12873551_10058987) – joaquin Apr 09 '12 at 22:07
  • check also [this](http://stackoverflow.com/questions/952914/making-a-flat-list-out-of-list-of-lists-in-python). – joaquin Apr 09 '12 at 22:16
  • @joaquin: I actually read that while writing my answer. Really, I'm just trying to explain to OP why his code is not working. I wrote the above code to flatten a (possibly) irregular list of arbritrary depth. Not sure why my answer deserves a downvote. – Joel Cornett Apr 09 '12 at 22:21
  • @JoelCornett: while your answer leaves a little something to be desired (ie. destroying the structure of the list), it's nothing that can't be remedied. You see, I need the second dimension for the very purpose of the program. However, I can just join every four entries in the final list together and then append the \n to it, to keep them together(that's what the sub-lists were for--organization). – Hersh S. Apr 10 '12 at 01:30
  • @HershS. : ah i see. well you would want to convert the portion you want to appear on a line to text. You can do this by calling `repr` on an object. `repr([['Aa'], ['Bb']])` will output a string `"[['Aa'], ['Bb']]"`. Do this for each item you would like on an individual line. Apparently Easygui textbox let's you use either a string or a list (of strings) so you can do`'\n'.join()` to convert the entire list to one string with newlines between each element, or just append a newline to each string in the list and pass the whole list as the argument. – Joel Cornett Apr 10 '12 at 01:31
  • @HershS.: I'll try to modify my answer when I can get near a computer in a bit. In the meantime see if `repr` works out. – Joel Cornett Apr 10 '12 at 01:35
  • In the end, I managed to work it out, using your flatten function and a list comprehension to join the items that would have been in a sub-list. thanks for the help! – Hersh S. Apr 10 '12 at 05:30
  • @JoelCornett downvote was for the reason indicated (use of comprehension instead of join). As it has been corected I return your points – joaquin Apr 10 '12 at 06:18