Posts: 5
Threads: 2
Joined: Nov 2016
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()
Posts: 5,151
Threads: 396
Joined: Sep 2016
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:
Posts: 5
Threads: 2
Joined: Nov 2016
(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.
Posts: 5,151
Threads: 396
Joined: Sep 2016
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:
Posts: 5
Threads: 2
Joined: Nov 2016
(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.
Posts: 5,151
Threads: 396
Joined: Sep 2016
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:
Posts: 5
Threads: 2
Joined: Nov 2016
(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!!
|