Python Forum
how to count elements in list box?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to count elements in list box?
#1
Hi 
Im wondering how to show how many elements in the list box in the status bar.
I know one could use  print (self.listbox.size()) but it shows up in the console.


from tkinter import *

class StatusBar(Frame):

    def __init__(self, master):
        Frame.__init__(self, master)
        self.label = Label(self, bd=1, relief=SUNKEN, anchor=W)
        self.label.pack(fill=X)

    def set(self, format, *args):
        self.label.config(text=format % args)
        self.label.update_idletasks()

    def clear(self):
        self.label.config(text="")
        self.label.update_idletasks()

class GUI:
    def __init__(self, master):
        self.master = master
        master.title("Test GUI")

        self.label = Label(master, text="Test GUI")
        self.label.pack()

        self.listbox = Listbox(master)
        self.listbox.pack()

        self.element = StringVar()
        
        self.Entry = Entry(master, textvariable=self.element)
        self.Entry.pack()
        
        self.close_button = Button(master, text="add", command=self.test)
        self.close_button.pack()

        status = StatusBar(root)
        status.pack(side=BOTTOM, fill=X)

    def test(self):
        value1 = (self.element.get())
        self.listbox.insert(END, value1)
        #shows how many items in list
        print (self.listbox.size()) 
        
root = Tk()
my_gui = GUI(root)
root.mainloop()
Reply
#2
Quote:but it shows up in the console
If you want it to show in the GUI you need to make a label for it, not print().

http://effbot.org/tkinterbook/label.htm
Recommended Tutorials:
Reply
#3
(Nov-02-2016, 08:42 PM)metulburr Wrote:
Quote:but it shows up in the console
If you want it to show in the GUI you need to make a label for it, not print().

http://effbot.org/tkinterbook/label.htm


Yes i know. i got one for it in class StatusBar. But what i meant was how do i make the label in there show how many elements there are in the listbox and every time i add a new one the number should change to show the new amount.
Reply
#4
If you look in the link i showed you, you will see a textvariable argument. This allows the text to change on the fly.
Recommended Tutorials:
Reply
#5
(Nov-03-2016, 04:55 AM)metulburr Wrote: If you look in the link i showed you, you will see a textvariable argument. This allows the text to change on the fly.

Ok but my problem is i dont know how to make it count the elements in the listbox and then show it in the label. i only know how to show in in the console.
Reply
#6
from tkinter import *
 
class StatusBar(Frame):
 
    def __init__(self, master):
        Frame.__init__(self, master)
        self.label = Label(self, bd=1, relief=SUNKEN, anchor=W)
        self.label.pack(fill=X)
 
    def set(self, format, *args):
        self.label.config(text=format % args)
        self.label.update_idletasks()
 
    def clear(self):
        self.label.config(text="")
        self.label.update_idletasks()
 
class GUI:
    def __init__(self, master):
        self.master = master
        master.title("Test GUI")
        self.var = IntVar()
        self.label = Label(master, textvariable=self.var)
        self.label.pack()
 
        self.listbox = Listbox(master)
        self.listbox.pack()
        self.var.set(self.listbox.size())
 
        self.element = StringVar()
         
        self.Entry = Entry(master, textvariable=self.element)
        self.Entry.pack()
         
        self.close_button = Button(master, text="add", command=self.test)
        self.close_button.pack()
 
        status = StatusBar(root)
        status.pack(side=BOTTOM, fill=X)
 
    def test(self):
        value1 = (self.element.get())
        self.listbox.insert(END, value1)
        #shows how many items in list
        #print (self.listbox.size()) 
        self.var.set(self.listbox.size())
         
root = Tk()
my_gui = GUI(root)
root.mainloop()
Recommended Tutorials:
Reply
#7
(Nov-03-2016, 05:31 AM)metulburr Wrote:
from tkinter import *
 
class StatusBar(Frame):
 
    def __init__(self, master):
        Frame.__init__(self, master)
        self.label = Label(self, bd=1, relief=SUNKEN, anchor=W)
        self.label.pack(fill=X)
 
    def set(self, format, *args):
        self.label.config(text=format % args)
        self.label.update_idletasks()
 
    def clear(self):
        self.label.config(text="")
        self.label.update_idletasks()
 
class GUI:
    def __init__(self, master):
        self.master = master
        master.title("Test GUI")
        self.var = IntVar()
        self.label = Label(master, textvariable=self.var)
        self.label.pack()
 
        self.listbox = Listbox(master)
        self.listbox.pack()
        self.var.set(self.listbox.size())
 
        self.element = StringVar()
         
        self.Entry = Entry(master, textvariable=self.element)
        self.Entry.pack()
         
        self.close_button = Button(master, text="add", command=self.test)
        self.close_button.pack()
 
        status = StatusBar(root)
        status.pack(side=BOTTOM, fill=X)
 
    def test(self):
        value1 = (self.element.get())
        self.listbox.insert(END, value1)
        #shows how many items in list
        #print (self.listbox.size()) 
        self.var.set(self.listbox.size())
         
root = Tk()
my_gui = GUI(root)
root.mainloop()
 Thank you it works great!!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020