By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,846 Members | 1,857 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,846 IT Pros & Developers. It's quick & easy.

Tk Search widget in a Lbox

bona25
P: 4
Hello! Can somebody help me in creating SEARCH button/widget in a tkinter listbox? I need to create one inside the listbox, thus, if input data/entry has been found, cursor will automatically be on the searched item.

Here's my current tk listbox:
Expand|Select|Wrap|Line Numbers
  1. from Tkinter import*
  2. import tkMessageBox
  3. import ttk
  4. import Tkinter
  5.  
  6.  
  7. def myListbox():
  8.     top = Tk()
  9.     top.title('This is it!')
  10.     top.geometry('200x150+200+150')
  11.  
  12.     Lb1 = Listbox(top)
  13.     Lb1.insert(1, "Aileen")
  14.     Lb1.insert(2, "Hazel")
  15.     Lb1.insert(3, "Rubie")
  16.     Lb1.insert(4, "Mizchell")
  17.     Lb1.insert(5, "Juvy")
  18.     Lb1.insert(6, "Arlene")
  19.     Lb1.insert(7, "Neil")
  20.     Lb1.insert(8, "Rey")
  21.     Lb1.insert(9, "Tricia")
  22.     Lb1.insert(10, "Alona")
  23.     Lb1.insert(11, "Melanie")
  24.     Lb1.insert(12, "Mecca")
  25.     Lb1.insert(13, "Ladera")
  26.     Lb1.insert(14, "Anna")
  27.  
  28.     Lb1.pack()
  29.     top.mainloop()     
  30.  
  31. def quitter():
  32.     if tkMessageBox.askokcancel('Verify Exit', 'Are you sure you want to quit?'):
  33.         quit()
  34.  
  35. app=Tk()
  36. app.title('My Tkinter')
  37. app.geometry('400x300+400+300')
  38.  
  39. button1=Button(app, text=' Listbox ', bg='white', fg='red',width=20, command=myListbox)
  40. buttonfont=('georgia',12,'bold')
  41. button1.config(font=buttonfont)
  42. button1.pack(side='top',padx=35, pady=55)
  43.  
  44. button1=Button(app, text='Logout', bg='maroon', fg='white',width=20, relief=RAISED, command=quitter)
  45. buttonfont=('georgia',12,'bold')
  46. button1.config(font=buttonfont)
  47. button1.pack(side='bottom', padx=15,pady=15)
  48.  
  49. app.mainloop()
Thanks for the help!!!!
Sep 22 '11 #1
Share this Question
Share on Google+
1 Reply


Expert 100+
P: 624
Use a list to hold the names. You can then use a list's index method to find the offset number, which you pass to itemconfig() to highlight the line, which means change the background color. Note that if you want to search for another name, you will have to reset the background color where necessary. Also, use Toplevel instead of 2 Tk() instances as 2 instances can confuse Tkinter as to which widget belongs to which instance. If you have not looked at classes yet you should, as this code would be much better in a class structure. A good Tkinter reference http://effbot.org/tkinterbook/
Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2. import tkMessageBox
  3. import ttk
  4. # import Tkinter       Duplicate import
  5.  
  6. def myListbox():
  7.     top = Toplevel()
  8.     top.title('This is it!')
  9.     top.geometry('200x150+200+150')
  10.  
  11.     list_of_names=["Aileen", "Hazel", "Rubie"]
  12.     Lb1 = Listbox(top)
  13.     for ctr, name in enumerate(list_of_names):
  14.          Lb1.insert(ctr, name)
  15.     Lb1.pack()
  16.  
  17.     ## simulate "Hazel" was selected
  18.     Lb1.itemconfig(1, background="lightgray")
  19.     top.mainloop() 
  20.  
  21. def quitter():
  22.     if tkMessageBox.askokcancel('Verify Exit', 'Are you sure you want to quit?'):
  23.         quit()
  24.  
  25. app=Tk()
  26. app.title('My Tkinter')
  27. app.geometry('400x300+400+300')
  28.  
  29. button1=Button(app, text=' Listbox ', bg='white', fg='red',width=20, command=myListbox)
  30. buttonfont=('georgia',12,'bold')
  31. button1.config(font=buttonfont)
  32. button1.pack(side='top',padx=35, pady=55)
  33.  
  34. button1=Button(app, text='Logout', bg='maroon', fg='white',width=20, relief=RAISED, command=quitter)
  35. buttonfont=('georgia',12,'bold')
  36. button1.config(font=buttonfont)
  37. button1.pack(side='bottom', padx=15,pady=15)
  38.  
  39. app.mainloop() 
Sep 22 '11 #2

Post your reply

Sign in to post your reply or Sign up for a free account.