Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I add a list of strings to a Tkinter Listbox without using a loop?

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.

like image 841
Jay Avatar asked Dec 14 '16 21:12

Jay


People also ask

How do I add data to a Listbox in Python?

A listbox shows a list of options. You can then click on any of those options. By default it won't do anything, but you can link that to a callback function or link a button click. To add new items, you can use the insert() method.

Which method is used to add elements in Listbox tkinter?

set () method of the horizontal scrollbar. Yscrollcommand - The yscrollcommand keyword used for the canvas is scrollable, this attribute must be the. set () method of the vertical scrollbar.

How do I update a Listbox in Python?

To edit the Listbox items, we have to first select the item in a loop using listbox. curselection() function and insert a new item after deleting the previous item in the listbox. To insert a new item in the listbox, you can use listbox. insert(**items) function.


1 Answers

This code inserts all strings in the collection:

listbox.insert(END, *strings)
like image 111
DYZ Avatar answered Oct 27 '22 00:10

DYZ