Home > other >  How to count items in listbox using tkinter?
How to count items in listbox using tkinter?

Time:02-01

I am having an issue with counting items in a list. I would like the number of counted items to be displayed in the window I created. How to do this with listbox.size() ? `

def count_tasks() listbox.index("end")

button_count_tasks = tkinter.Button(root, text="Count tasks", width=48, command=count_tasks) button_count_tasks.pack()`

how to make it work? and how to show results in some box?

CodePudding user response:

You can ask the listbox to return you the index of the last element:

the_listbox.index("end")

The number that is returned corresponds to the number of items in the listbox.

If you want to show it in "some box", you just need to insert the box or configure it, depending on what you mean by "some box". If it's an existing label then it would be something like:

count = the_listbox.index("end")
the_label.configure(text=f"{count} items in listbox")
  •  Tags:  
  • Related