How to filter data using listbox in python from using csv file using TkTreectrl library
Pakistan Beauty <[email protected]> Tue, 31 Mar 2020 11:37:53 +0500
| Newsgroups | gmane.comp.python.tkinter |
|---|---|
| Message-ID | <CAFi14bA=NvZfOG78KdxFA6t2xoXn0twtRCg11BMpG85Vnp-5SQ@mail.gmail.com> |
--===============5596559990770688217==
Content-Type: multipart/alternative; boundary="00000000000057ce8305a220cf28"
--00000000000057ce8305a220cf28
Content-Type: text/plain; charset="UTF-8"
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()
#####################################################################
--00000000000057ce8305a220cf28
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">=C2=A0I have this list box=C2=A0<div><br></div><div><pre s=
tyle=3D"color:rgb(0,0,0);font-family:Consolas;font-size:13.5pt"><br><span s=
tyle=3D"color:rgb(128,128,128);font-style:italic">#########################=
##############################################<br></span><span style=3D"col=
or:rgb(128,128,128);font-style:italic"><br></span><span style=3D"color:rgb(=
0,0,128);font-weight:bold">import </span>tkinter<br><span style=3D"color:rg=
b(0,0,128);font-weight:bold">from </span>TkTreectrl <span style=3D"color:rg=
b(0,0,128);font-weight:bold">import </span>*<br><br><span style=3D"color:rg=
b(128,128,128);font-style:italic"># define some data to insert into the lis=
t:<br></span>data =3D {<br> <span style=3D"color:rgb(0,128,128);font-wei=
ght:bold">'Joe'</span>: <span style=3D"color:rgb(0,0,255)">100=
0.00</span>,<br> <span style=3D"color:rgb(0,128,128);font-weight:bold">&=
#39;Jill'</span>: <span style=3D"color:rgb(0,0,255)">738.39</span>,=
<br> <span style=3D"color:rgb(0,128,128);font-weight:bold">'Jack'=
;</span>: <span style=3D"color:rgb(0,0,255)">625.11</span>,<br> <spa=
n style=3D"color:rgb(0,128,128);font-weight:bold">'Jane'</span>: =
<span style=3D"color:rgb(0,0,255)">99.99<br></span>}<br><br><br>root =3D =
tkinter.Tk()<br><span style=3D"color:rgb(128,128,128);font-style:italic"># =
create list with scrollbars<br></span>slistbox =3D ScrolledMultiListbox(roo=
t)<br>slistbox.pack(<span style=3D"color:rgb(102,0,153)">fill</span>=3D<spa=
n style=3D"color:rgb(0,128,128);font-weight:bold">'both'</span>, <s=
pan style=3D"color:rgb(102,0,153)">expand</span>=3D<span style=3D"color:rgb=
(0,0,255)">1</span>)<br>listbox =3D slistbox.listbox<br><span style=3D"colo=
r:rgb(128,128,128);font-style:italic"># create columns<br></span>listbox.co=
nfigure(<span style=3D"color:rgb(102,0,153)">columns</span>=3D(<span style=
=3D"color:rgb(0,128,128);font-weight:bold">'Paid'</span>, <span sty=
le=3D"color:rgb(0,128,128);font-weight:bold">'Customer'</span>, <sp=
an style=3D"color:rgb(0,128,128);font-weight:bold">'Debt'</span>))<=
br><br><span style=3D"color:rgb(128,128,128);font-style:italic">#### add ch=
eckbox support to the listbox ####<br></span><span style=3D"color:rgb(128,1=
28,128);font-style:italic"># checkbox icons:<br></span>checked =3D tkinter.=
PhotoImage(<span style=3D"color:rgb(102,0,153)">data</span>=3D(<span style=
=3D"color:rgb(0,128,128);font-weight:bold">r'R0lGODlhDQANABEAACwAAAAADQ=
ANAIEAAAB/f3/f3'<br></span><span style=3D"color:rgb(0,128,128);font-wei=
ght:bold"> '9////8CJ4yPNgHtLxYYtNbIbJ146jZ0gzeCIuhQ53NJVNpmryZqsYDne=
mT3BQA7'</span>))<br>unchecked =3D tkinter.PhotoImage(<span style=3D"co=
lor:rgb(102,0,153)">data</span>=3D<span style=3D"color:rgb(0,128,128);font-=
weight:bold">'R0lGODlhDQANABEAACwAAAAADQANAIEAAAB/f3'<br></span><sp=
an style=3D"color:rgb(0,128,128);font-weight:bold"> '/f39////8CIYyPN=
gHtLxYYtNbIrMZTX+l9WThwZAmSppqGmADHcnRaBQA7'</span>)<br><br><span style=
=3D"color:rgb(128,128,128);font-style:italic"># create treectrl state, elem=
ent and style for the checkboxes:<br></span>listbox.state_define(<span styl=
e=3D"color:rgb(0,128,128);font-weight:bold">'Checked'</span>)<br>im=
gCheck =3D listbox.element_create(<br> <span style=3D"color:rgb(=
102,0,153)">type</span>=3D<span style=3D"color:rgb(0,128,128);font-weight:b=
old">'image'</span>, <span style=3D"color:rgb(102,0,153)">image</sp=
an>=3D(checked, <span style=3D"color:rgb(0,128,128);font-weight:bold">'=
Checked'</span>, unchecked, ()))<br><br>icon_style =3D listbox.style_cr=
eate()<br>listbox.style_elements(icon_style,<br> listbox.element=
(<span style=3D"color:rgb(0,128,128);font-weight:bold">'select'</sp=
an>), listbox.element(<span style=3D"color:rgb(0,128,128);font-weight:bold"=
>'text'</span>),imgCheck)<br>listbox.style_layout(icon_style, imgCh=
eck, <span style=3D"color:rgb(102,0,153)">padx</span>=3D<span style=3D"colo=
r:rgb(0,0,255)">3</span>, <span style=3D"color:rgb(102,0,153)">pady</span>=
=3D<span style=3D"color:rgb(0,0,255)">2</span>)<br>listbox.style(<span styl=
e=3D"color:rgb(0,0,255)">0</span>, icon_style)<br><br><br><span style=3D"co=
lor:rgb(128,128,128);font-style:italic"># define checkbox callback that wil=
l be bound to mouse-button-1 events:<br></span><span style=3D"color:rgb(0,0=
,128);font-weight:bold">def </span>cmd(event):<br> <span style=3D"color:=
rgb(128,128,128);font-style:italic"># check which element was clicked<br></=
span><span style=3D"color:rgb(128,128,128);font-style:italic"> </span>id=
entify =3D listbox.identify(event.x, event.y)<br> <span style=3D"color:r=
gb(128,128,128);font-style:italic"># identify might be None or look like:<b=
r></span><span style=3D"color:rgb(128,128,128);font-style:italic"> # (&#=
39;item', '2', 'column', '0') or<br></span><spa=
n style=3D"color:rgb(128,128,128);font-style:italic"> # ('item',=
'3', 'column', '0', 'elem', 'pyelement=
3')<br></span><span style=3D"color:rgb(128,128,128);font-style:italic">=
</span><span style=3D"color:rgb(0,0,128);font-weight:bold">if </span>id=
entify:<br> <span style=3D"color:rgb(0,0,128);font-weight:bold">try<=
/span>:<br> item, column, element =3D identify[<span style=3D"co=
lor:rgb(0,0,255)">1</span>], identify[<span style=3D"color:rgb(0,0,255)">3<=
/span>], identify[<span style=3D"color:rgb(0,0,255)">5</span>]<br> =
<span style=3D"color:rgb(0,0,128);font-weight:bold">if </span>element =
=3D=3D imgCheck:<br> <span style=3D"color:rgb(128,128,128);f=
ont-style:italic"># toggle the "checked" state<br></span><span st=
yle=3D"color:rgb(128,128,128);font-style:italic"> </span>lis=
tbox.itemstate_forcolumn(item, column, <span style=3D"color:rgb(0,128,128);=
font-weight:bold">'~Checked'</span>)<br> <span style=
=3D"color:rgb(128,128,128);font-style:italic"># do something, dependent on =
the current state of the checkbox<br></span><span style=3D"color:rgb(128,12=
8,128);font-style:italic"> </span>new =3D listbox.itemstate_=
forcolumn(item, column)<br> <span style=3D"color:rgb(0,0,128=
);font-weight:bold">if </span>new <span style=3D"color:rgb(0,0,128);font-we=
ight:bold">and </span><span style=3D"color:rgb(0,128,128);font-weight:bold"=
>'Checked' </span><span style=3D"color:rgb(0,0,128);font-weight:bol=
d">in </span>new:<br> <span style=3D"color:rgb(128,128,1=
28);font-style:italic"># the checkbox was newly checked<br></span><span sty=
le=3D"color:rgb(128,128,128);font-style:italic"> </span>=
<span style=3D"color:rgb(0,0,128)">print</span>(<span style=3D"color:rgb(0,=
128,128);font-weight:bold">'Checked item'</span>, item, <span style=
=3D"color:rgb(0,128,128);font-weight:bold">'in column'</span>, colu=
mn)<br> <span style=3D"color:rgb(128,128,128);font-style=
:italic"># example: debts are paid, set "Debt" to 0.00<br></span>=
<span style=3D"color:rgb(128,128,128);font-style:italic"> =
</span>listbox.itemelement_configure(item, listbox.column(<span style=3D"=
color:rgb(0,0,255)">2</span>),<br> listb=
ox.element(<span style=3D"color:rgb(0,128,128);font-weight:bold">'text&=
#39;</span>), <span style=3D"color:rgb(102,0,153)">text</span>=3D<span styl=
e=3D"color:rgb(0,128,128);font-weight:bold">'0.00'</span>)<br> =
<span style=3D"color:rgb(0,0,128);font-weight:bold">else</span>:=
<br> <span style=3D"color:rgb(128,128,128);font-style:it=
alic"># example: no payment received, set debt to stored value<br></span><s=
pan style=3D"color:rgb(128,128,128);font-style:italic"> =
</span><span style=3D"color:rgb(0,0,128)">print</span>(<span style=3D"color=
:rgb(0,128,128);font-weight:bold">'Unchecked item'</span>, item, <s=
pan style=3D"color:rgb(0,128,128);font-weight:bold">'in column'</sp=
an>, column)<br> customer =3D listbox.itemelement_cget(<=
br> item, listbox.column(<span style=3D"=
color:rgb(0,0,255)">1</span>),<br> listb=
ox.element(<span style=3D"color:rgb(0,128,128);font-weight:bold">'text&=
#39;</span>), <span style=3D"color:rgb(0,128,128);font-weight:bold">'te=
xt'</span>)<br> debt =3D data[customer]<br> =
listbox.itemelement_configure(item, listbox.column(<span style=
=3D"color:rgb(0,0,255)">2</span>),<br> l=
istbox.element(<span style=3D"color:rgb(0,128,128);font-weight:bold">'t=
ext'</span>), <span style=3D"color:rgb(102,0,153)">text</span>=3Ddebt)<=
br> <span style=3D"color:rgb(0,0,128);font-weight:bold">except </spa=
n><span style=3D"color:rgb(0,0,128)">IndexError</span>:<br> <spa=
n style=3D"color:rgb(128,128,128);font-style:italic"># we did not hit the c=
heckbox, never mind<br></span><span style=3D"color:rgb(128,128,128);font-st=
yle:italic"> </span><span style=3D"color:rgb(0,0,128);font-weigh=
t:bold">pass<br></span><span style=3D"color:rgb(0,0,128);font-weight:bold">=
<br></span><span style=3D"color:rgb(128,128,128);font-style:italic"># bind =
the callback to button 1 events<br></span>listbox.bind(<span style=3D"color=
:rgb(0,128,128);font-weight:bold">'<1>'</span>, cmd)<br><br><=
span style=3D"color:rgb(128,128,128);font-style:italic"># insert data into =
the list to see if this works:<br></span><span style=3D"color:rgb(0,0,128);=
font-weight:bold">for </span>customer <span style=3D"color:rgb(0,0,128);fon=
t-weight:bold">in </span>data:<br> listbox.insert(<span style=3D"color:r=
gb(0,128,128);font-weight:bold">'end'</span>, <span style=3D"color:=
rgb(0,128,128);font-weight:bold">''</span>, customer, data[customer=
])<br><br><br>root.mainloop()<br><br><span style=3D"color:rgb(128,128,128);=
font-style:italic">########################################################=
#######################<br></span><span style=3D"color:rgb(128,128,128);fon=
t-style:italic"><br></span><span style=3D"color:rgb(128,128,128);font-style=
:italic">And I want to use this search box function in this list box</span>=
</pre><pre style=3D"color:rgb(0,0,0);font-family:Consolas;font-size:13.5pt"=
><pre style=3D"font-family:Consolas;font-size:13.5pt"><span style=3D"color:=
rgb(128,128,128);font-style:italic">#######################################=
####################################333<br></span><span style=3D"color:rgb(=
128,128,128);font-style:italic"><br></span><span style=3D"color:rgb(0,0,128=
);font-weight:bold">from </span>tkinter <span style=3D"color:rgb(0,0,128);f=
ont-weight:bold">import </span>*<br><br><br><span style=3D"color:rgb(128,12=
8,128);font-style:italic"># First create application class<br></span><span =
style=3D"color:rgb(128,128,128);font-style:italic"><br></span><span style=
=3D"color:rgb(128,128,128);font-style:italic"><br></span><span style=3D"col=
or:rgb(0,0,128);font-weight:bold">class </span>Application(Frame):<br><br> =
<span style=3D"color:rgb(0,0,128);font-weight:bold">def </span><span sty=
le=3D"color:rgb(178,0,178)">__init__</span>(<span style=3D"color:rgb(148,85=
,141)">self</span>, master=3D<span style=3D"color:rgb(0,0,128);font-weight:=
bold">None</span>):<br> Frame.<span style=3D"color:rgb(178,0,178)">_=
_init__</span>(<span style=3D"color:rgb(148,85,141)">self</span>, master)<b=
r><br> <span style=3D"color:rgb(148,85,141)">self</span>.pack()<br> =
<span style=3D"color:rgb(148,85,141)">self</span>.create_widgets()<b=
r><br> <span style=3D"color:rgb(128,128,128);font-style:italic"># Create=
main GUI window<br></span><span style=3D"color:rgb(128,128,128);font-style=
:italic"> </span><span style=3D"color:rgb(0,0,128);font-weight:bold">def=
</span>create_widgets(<span style=3D"color:rgb(148,85,141)">self</span>):<=
br> <span style=3D"color:rgb(148,85,141)">self</span>.search_var =3D=
StringVar()<br> <span style=3D"color:rgb(148,85,141)">self</span>.s=
earch_var.trace(<span style=3D"color:rgb(0,128,128);font-weight:bold">"=
;w"</span>, <span style=3D"color:rgb(0,0,128);font-weight:bold">lambda=
</span>name, index, mode: <span style=3D"color:rgb(148,85,141)">self</span=
>.update_list())<br> <span style=3D"color:rgb(148,85,141)">self</spa=
n>.entry =3D Entry(<span style=3D"color:rgb(148,85,141)">self</span>, <span=
style=3D"color:rgb(102,0,153)">textvariable</span>=3D<span style=3D"color:=
rgb(148,85,141)">self</span>.search_var, <span style=3D"color:rgb(102,0,153=
)">width</span>=3D<span style=3D"color:rgb(0,0,255)">13</span>)<br> =
<span style=3D"color:rgb(148,85,141)">self</span>.lbox =3D Listbox(<span st=
yle=3D"color:rgb(148,85,141)">self</span>, <span style=3D"color:rgb(102,0,1=
53)">width</span>=3D<span style=3D"color:rgb(0,0,255)">45</span>, <span sty=
le=3D"color:rgb(102,0,153)">height</span>=3D<span style=3D"color:rgb(0,0,25=
5)">15</span>)<br><br> <span style=3D"color:rgb(148,85,141)">self</s=
pan>.entry.grid(<span style=3D"color:rgb(102,0,153)">row</span>=3D<span sty=
le=3D"color:rgb(0,0,255)">0</span>, <span style=3D"color:rgb(102,0,153)">co=
lumn</span>=3D<span style=3D"color:rgb(0,0,255)">0</span>, <span style=3D"c=
olor:rgb(102,0,153)">padx</span>=3D<span style=3D"color:rgb(0,0,255)">10</s=
pan>, <span style=3D"color:rgb(102,0,153)">pady</span>=3D<span style=3D"col=
or:rgb(0,0,255)">3</span>)<br> <span style=3D"color:rgb(148,85,141)"=
>self</span>.lbox.grid(<span style=3D"color:rgb(102,0,153)">row</span>=3D<s=
pan style=3D"color:rgb(0,0,255)">1</span>, <span style=3D"color:rgb(102,0,1=
53)">column</span>=3D<span style=3D"color:rgb(0,0,255)">0</span>, <span sty=
le=3D"color:rgb(102,0,153)">padx</span>=3D<span style=3D"color:rgb(0,0,255)=
">10</span>, <span style=3D"color:rgb(102,0,153)">pady</span>=3D<span style=
=3D"color:rgb(0,0,255)">3</span>)<br><br> <span style=3D"color:rgb(1=
28,128,128);font-style:italic"># Function for updating the list/doing the s=
earch.<br></span><span style=3D"color:rgb(128,128,128);font-style:italic"> =
# It needs to be called here to populate the listbox.<br></span><spa=
n style=3D"color:rgb(128,128,128);font-style:italic"> </span><span s=
tyle=3D"color:rgb(148,85,141)">self</span>.update_list()<br><br> <span s=
tyle=3D"color:rgb(0,0,128);font-weight:bold">def </span>update_list(<span s=
tyle=3D"color:rgb(148,85,141)">self</span>):<br> search_term =3D <sp=
an style=3D"color:rgb(148,85,141)">self</span>.search_var.get()<br><br> =
<span style=3D"color:rgb(128,128,128);font-style:italic"># Just a gener=
ic list to populate the listbox<br></span><span style=3D"color:rgb(128,128,=
128);font-style:italic"> </span>lbox_list =3D [<span style=3D"color:=
rgb(0,128,128);font-weight:bold">'Adam'</span>, <span style=3D"colo=
r:rgb(0,128,128);font-weight:bold">'Lucy'</span>, <span style=3D"co=
lor:rgb(0,128,128);font-weight:bold">'Barry'</span>, <span style=3D=
"color:rgb(0,128,128);font-weight:bold">'Bob'</span>,<br> =
<span style=3D"color:rgb(0,128,128);font-weight:bold">'James=
'</span>, <span style=3D"color:rgb(0,128,128);font-weight:bold">'Fr=
ank'</span>, <span style=3D"color:rgb(0,128,128);font-weight:bold">'=
;Susan'</span>, <span style=3D"color:rgb(0,128,128);font-weight:bold">&=
#39;Amanda'</span>, <span style=3D"color:rgb(0,128,128);font-weight:bol=
d">'Christie'</span>]<br><br> <span style=3D"color:rgb(148,8=
5,141)">self</span>.lbox.delete(<span style=3D"color:rgb(0,0,255)">0</span>=
, END)<br><br> <span style=3D"color:rgb(0,0,128);font-weight:bold">f=
or </span>item <span style=3D"color:rgb(0,0,128);font-weight:bold">in </spa=
n>lbox_list:<br> <span style=3D"color:rgb(0,0,128);font-weight:b=
old">if </span>search_term.lower() <span style=3D"color:rgb(0,0,128);font-w=
eight:bold">in </span>item.lower():<br> <span style=3D"color=
:rgb(148,85,141)">self</span>.lbox.insert(END, item)<br><br><br>root =3D Tk=
()<br>root.title(<span style=3D"color:rgb(0,128,128);font-weight:bold">'=
;Filter Listbox Test'</span>)<br>app =3D Application(<span style=3D"col=
or:rgb(102,0,153)">master</span>=3Droot)<br><span style=3D"color:rgb(0,0,12=
8)">print</span>(<span style=3D"color:rgb(0,128,128);font-weight:bold">'=
;Starting mainloop()'</span>)<br>app.mainloop()<br><br><span style=3D"c=
olor:rgb(128,128,128);font-style:italic">##################################=
###################################</span></pre></pre></div></div>
--00000000000057ce8305a220cf28--
--===============5596559990770688217==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
_______________________________________________
Tkinter-discuss mailing list
[email protected]
https://mail.python.org/mailman/listinfo/tkinter-discuss
--===============5596559990770688217==--