5

I'm trying to understand what the fastest way is to add a bunch of strings that are pre-arranged in some fashion to a Listbox widget, so that every string is in a new line.

The quickest I could gather so far:

 from Tkinter import *

 strings= 'str1', 'str2', 'str3'
 listbox=Listbox(None)
 [listbox.insert(END, item) for item in strings]
 listbox.pack()

Is there perhaps a cleaner faster way to get it done, without iterating over every string? Perhaps if the strings are pre-packed in a certain way or using some other method?

If it is of relevance, I want to use it to display directory listings.

2
  • What's END in your case? Commented Dec 14, 2016 at 21:25
  • @ettanany, that's the current end of the listbox as far as I understand from Programming Python, so basically allows inserting without being concerned with the current index Commented Dec 14, 2016 at 21:36

2 Answers 2

6

This code inserts all strings in the collection:

listbox.insert(END, *strings)
Sign up to request clarification or add additional context in comments.

4 Comments

Sweet, thanks. Just to make sure. is this the same usage of * as in *args/**kwargs to a function? I ask because if I remember correctly, I use *args in the function definition but not when calling it.
@Joe The Python doc Index has a page called Symbols. Under the * heading is an entry in function calls, which links to docs.python.org/3/reference/expressions.html#index-38, which says "If the syntax *expression appears in the function call, expression must evaluate to an iterable. Elements from these iterables are treated as if they were additional positional arguments. ...", followed by more examples and explanations.
Thanks @TerryJanReedy, I also found this in the python control flow docs -
link - "The reverse situation occurs when the arguments are already in a list or tuple but need to be unpacked for a function call requiring separate positional arguments.. If they are not available separately, write the function call with the *-operator to unpack the arguments out of a list or tuple". So basically it is an unpacking operator. Sweet.
0

The problem is not so simple. In most cases you don't insert a simple string, but a formatted string made from elements of a list or tuple. I solved that problem in such a way as follows. I inserted e.g. 20 items to fill the listbox for the first time and added an <> event with a hidden counter ( an invisible label with same foreground color and background color ) to the listbox. The listbox was filled immediately without any waiting time what was very frustrating for users. After you entered the listbox with the mouse and the label.cget("text") was e.g. "0" the filling of the listbox with the whole list was proceeded.It took time but the user thought it was normal. Sure the label's text (the counter) must be changed to e.g "1" or to other string different to the former.

1 Comment

Welcome to Stack Overflow. Please read How to Answer.

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.