473,394 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Tk Search widget in a Lbox

bona25
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
1 5399
dwblas
626 Expert 512MB
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

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

Similar topics

2
by: Tonino | last post by:
Hi, I have a small Tkinter app that gets data from a socket connection to a "server". The app has a Text() widget to display the info that it gets from the socket connection. I have the...
6
by: William Gill | last post by:
I am trying to get & set the properties of a widget's parent widget. What I have works, but seems like a long way around the block. First I get the widget name using w.winfo_parent(), then i...
4
by: Ken Fine | last post by:
I'm looking to find or create an ASP script that will take a string, examine it for a search term, and if it finds the search term in the string, return the highlighted search term along with the...
3
by: mark | r | last post by:
we have a MSAccess database of products with titles, short descriptions, long descriotions, bulletpoints etc contained for each product. we currently search the database looking for the target...
1
by: Avatar Viper | last post by:
I have an xml file: <?xml version="1.0"?> <ProductList> <Products> <SKU>1</SKU> <Price>100.00</Price> <Description>Widget #1</Description> <pubDate>8/6/2004</pubDate> </Products>
3
by: David Ross | last post by:
Please direct me to a Web site that describes how to implement a search engine for my own Web site. -- David E. Ross <URL:http://www.rossde.com/> I use Mozilla as my Web browser because I...
0
by: Bob Greschke | last post by:
I want to create a search function on a Text() widget full of text like the incremental search in emacs -- if you type an "f" emacs goes to the first "f", type another "f" and it goes to the first...
3
by: mariox19 | last post by:
Are Tkinter widgets running on their own thread? If I try to make a simple application that will print the letters A to Z to a Tkinter Text widget, and I space the printing of each letter by 1...
1
by: jamiil | last post by:
One of the most beautiful aspects of living this times is that most of the walls have come down. The wall that existed between Unix and MS-Windows has, greatly, fallen, so, when writing a program...
15
by: maya | last post by:
hi, this site, http://www.mta.info/lirr/ has a "google translate" widget (on left column); when you click on drop-down some languages don't display properly (encoding problem), this problem...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.