I have this list box
#######################################################################
import tkinter
from TkTreectrl import *
# define some data to insert into the list:
data = {
'Joe': 1000.00,
'Jill': 738.39,
'Jack': 625.11,
'Jane': 99.99
}
root = tkinter.Tk()
# create list with scrollbars
slistbox = ScrolledMultiListbox(root)
slistbox.pack(fill='both', expand=1)
listbox = slistbox.listbox
# create columns
listbox.configure(columns=('Paid', 'Customer', 'Debt'))
#### add checkbox support to the listbox ####
# checkbox icons:
checked = tkinter.PhotoImage(data=(r'R0lGODlhDQANABEAACwAAAAADQANAIEAAAB/f3/f3'
'9////8CJ4yPNgHtLxYYtNbIbJ146jZ0gzeCIuhQ53NJVNpmryZqsYDnemT3BQA7'))
unchecked = tkinter.PhotoImage(data='R0lGODlhDQANABEAACwAAAAADQANAIEAAAB/f3'
'/f39////8CIYyPNgHtLxYYtNbIrMZTX+l9WThwZAmSppqGmADHcnRaBQA7')
# create treectrl state, element and style for the checkboxes:
listbox.state_define('Checked')
imgCheck = listbox.element_create(
type='image', image=(checked, 'Checked', unchecked, ()))
icon_style = listbox.style_create()
listbox.style_elements(icon_style,
listbox.element('select'), listbox.element('text'),imgCheck)
listbox.style_layout(icon_style, imgCheck, padx=3, pady=2)
listbox.style(0, icon_style)
# define checkbox callback that will be bound to mouse-button-1 events:
def cmd(event):
# check which element was clicked
identify = listbox.identify(event.x, event.y)
# identify might be None or look like:
# ('item', '2', 'column', '0') or
# ('item', '3', 'column', '0', 'elem', 'pyelement3')
if identify:
try:
item, column, element = identify[1], identify[3], identify[5]
if element == imgCheck:
# toggle the "checked" state
listbox.itemstate_forcolumn(item, column, '~Checked')
# do something, dependent on the current state of the checkbox
new = listbox.itemstate_forcolumn(item, column)
if new and 'Checked' in new:
# the checkbox was newly checked
print('Checked item', item, 'in column', column)
# example: debts are paid, set "Debt" to 0.00
listbox.itemelement_configure(item, listbox.column(2),
listbox.element('text'), text='0.00')
else:
# example: no payment received, set debt to stored value
print('Unchecked item', item, 'in column', column)
customer = listbox.itemelement_cget(
item, listbox.column(1),
listbox.element('text'), 'text')
debt = data[customer]
listbox.itemelement_configure(item, listbox.column(2),
listbox.element('text'), text=debt)
except IndexError:
# we did not hit the checkbox, never mind
pass
# bind the callback to button 1 events
listbox.bind('<1>', cmd)
# insert data into the list to see if this works:
for customer in data:
listbox.insert('end', '', customer, data[customer])
root.mainloop()
###############################################################################
And I want to use this search box function in this list box
###########################################################################333
from tkinter import *
# First create application class
class Application(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
self.create_widgets()
# Create main GUI window
def create_widgets(self):
self.search_var = StringVar()
self.search_var.trace("w", lambda name, index, mode: self.update_list())
self.entry = Entry(self, textvariable=self.search_var, width=13)
self.lbox = Listbox(self, width=45, height=15)
self.entry.grid(row=0, column=0, padx=10, pady=3)
self.lbox.grid(row=1, column=0, padx=10, pady=3)
# Function for updating the list/doing the search.
# It needs to be called here to populate the listbox.
self.update_list()
def update_list(self):
search_term = self.search_var.get()
# Just a generic list to populate the listbox
lbox_list = ['Adam', 'Lucy', 'Barry', 'Bob',
'James', 'Frank', 'Susan', 'Amanda', 'Christie']
self.lbox.delete(0, END)
for item in lbox_list:
if search_term.lower() in item.lower():
self.lbox.insert(END, item)
root = Tk()
root.title('Filter Listbox Test')
app = Application(master=root)
print('Starting mainloop()')
app.mainloop()
#####################################################################
_______________________________________________
Tkinter-discuss mailing list
[email protected]
https://mail.python.org/mailman/listinfo/tkinter-discuss