Adding items to Listbox in Python Tkinter

2024/10/5 15:20:31

I would like my Listbox widget to be updated upon clicking of a button. However I encountered a logic error. When I click on the button, nothing happens. No errors at all.

listOfCompanies: [[1, ''], [2, '-'], [3, '@ASK TRAINING PTE. LTD.'], [4, 'AAIS'], [5, 'Ademco'], [6, 'Anacle']def populatebox():listBox.insert("end", listOfCompanies)btn = Button(self, text="Update list", command = lambda: populatebox())
btn.pack()
Answer

If you're looking to just insert every tuple into the Listbox from the list as they are without separating out the tuple then there are two major changes.

First you cannot declare a list as list: [1, 2, 3, ...], it must be list = [1, 2, 3, ...].

Secondly, you are currently attempting to insert the entire list onto one entry in the Listbox. You should instead iterate over them, see below for an example:

from tkinter import *root = Tk()listBox = Listbox(root)
listBox.pack()listOfCompanies = [[1, ''], [2, '-'], [3, '@ASK TRAINING PTE. LTD.'], [4, 'AAIS'], [5, 'Ademco'], [6, 'Anacle']]def populatebox():for i in listOfCompanies:listBox.insert("end", i)btn = Button(root, text="Update list", command = lambda: populatebox())
btn.pack()
https://en.xdnf.cn/q/119911.html

Related Q&A

Policy based design in Python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 9 years ago.Improve…

Exception raised: cannot import name manual_seed from torch

im trying to run the AutoClean project on my device (heres my code): import random from AutoClean import AutoClean import pandas as pddef __init__(self, pipeline, resultat ):self.pipeline = pipelinesel…

Compile a C/C++ Program and store standard output in a File via Python

Lets say I have a C/C++ file named userfile.c. Using Python, how can I invoke the local gcc compiler so that the file is compiled and an executable is made? More specifically, I would like to provide …

How to swap maximums with the minimums? (python)

Is there a method to swap the maximum and the minimum of a list? The list will be as follows and the program has to be continued so that it will print the maximum swapped with the minimum, the second …

python object attributes and methods

In python all data is object and any object should have attributes and methods. Does somebody know python object without any attributes and methods?>>> len(dir(1)) 64

How to retrieve nested data with BeautifulSoup?

I have the below webpage source: </li><li class="cl-static-search-result" title="BELLO HONDA ACCORD &quot;95 MIL MILLAS&quot;. REALMENTE COMO NUEVO"><a href=&…

applying onehotencoder on numpy array

I am applying OneHotEncoder on numpy array.Heres the codeprint X.shape, test_data.shape #gives 4100, 15) (410, 15) onehotencoder_1 = OneHotEncoder(categorical_features = [0, 3, 4, 5, 6, 8, 9, 11, 12]) …

How to delete temp folder data using python script [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 6…

Save a list of objects on exit of pygame game [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 10 years ago.Improv…

Trying to make loop for a function that stops after the result is lower than a certain value

Im taking a beginner python class and part of an exercise we were given was this:The point x with the property x= sin(x)−ax+ 30 is called a fixed point of the function f(x) = sin(x)−ax+ 30. It can b…