Header Ads

Header ADS

creating Listbox in tkinter


Tkinter Listbox 

The listbox widget is used to display the list items to the user .
the user can choose one or more items from the list depending upon the configuration.

the list box is looks similar to text box but in list box user cannot change the value or modify the content written in List box as we do in text box .

lust box is the preferred widget for data presentation in almost all GUI Frame works .
syntax :::

   w = Listbox(parent, options)   

In this Article we will create Two  3 types of listbox ..


1 . create and fetch Single selected data from list box :


# tkinter : Dynamic Coding
# listbox ..

# import Library ...
from tkinter import *

def func():
	lbl['text'] = listbox.get(ACTIVE)
	

root = Tk()
root.title("Dynamic Coding")

lbl = Label(root,text='',font=('arial',12,'bold'),fg='cyan')
lbl.pack(pady=10)
# creating listbox
listbox = Listbox(root,font=('arial',12,'bold'),selectmode=SINGLE)
listbox.pack(pady=10)
# inserting data into it 
listbox.insert(END,'Python')
listbox.insert(END,'C++')
listbox.insert(END,'Java')
listbox.insert(END,'HTML')

# creating button for function call
Button(root,text='Get Data',command=func).pack(pady=10)

root.mainloop()


Output :

In the following Listbox when we select any value and press the button then the selected values is replaces the text of label .


2 . create and fetch Multiple selected data from list box :

# tkinter : Dynamic Coding
# listbox ..

# import Library ...
from tkinter import *

def get_selection():
    items = [languages[int(item)] 
    for item in LanguageList.curselection()]
    print(items)

root = Tk()
root.title("Dynamic Coding")

# creating listbox ...
LanguageList = Listbox(root)
LanguageList.configure(selectmode=MULTIPLE,
 width=9, height=5)
LanguageList.pack(pady=10)

btnGet = Button(root,text="Get Selection"
	,command=get_selection)
btnGet.pack(pady=5)
# data ti be inserted into list box ..
languages = ["C++", "Python", "Java", "HTML", "Kotlin"]

# inserting data into listbox ....
for lang in languages:
    LanguageList.insert(END, lang)

root.mainloop()


Output :



3 .Bind Mouse event to listbox .

# tkinter : Dynamic Coding
# listbox ..

# import Library ...
from tkinter import *
   
def go(event): 
    cs = Lb.get(ANCHOR)
    w['text'] = cs
    top.configure(bg=cs)
   
top = Tk() 
top.geometry('250x275') 
top.title('Dynamic Coding') 
   
# Creating Listbox 
Lb = Listbox(top, height=6) 
# Inserting items in Listbox 
Lb.insert(0, 'Red') 
Lb.insert(1, 'Green') 
Lb.insert(2, 'Yellow') 
Lb.insert(3, 'White') 
   
# Binding double click with left mouse 
# button with go function 
#Lb.bind('<Button-1>', go) 
Lb.bind('<<ListboxSelect>>', go)
Lb.pack(pady=10) 
   
# Creating Edit box to show selected option 
w = Label(top, text='Default') 
w.pack(pady=5) 
top.mainloop()

Output :




Attributes / options of Listbox .....

1 .bg : The background color of the widget.

2. bd : It represents the size of the border. Default value is 2 pixel.

3. cursor : The mouse pointer will look like the cursor type like dot, arrow, etc.

4. font : The font type of the Listbox items.

5. fg : The color of the text.

6. height : It represents the count of the lines shown in the Listbox. The default value is 10.

7. highlightcolor : The color of the Listbox items when the widget is under focus.

8. highlightthickness : The thickness of the highlight.

9. relief : The type of the border. The default is SUNKEN.

10. selectbackground : The background color that is used to display the selected text.

11. selectmode : It is used to determine the number of items that can be selected from the list. It can set to BROWSE, SINGLE, MULTIPLE, EXTENDED.
12. width It represents the width of the widget in characters.

13. scrollcommand : It is used to let the user scroll the Listbox horizontally.

14. yscrollcommand : It is used to let the user scroll the Listbox vertically.
Methods


Methods of Listbox ...

1. activate(index) : It is used to select the lines at the specified index.

2. curselection() : It returns a tuple containing the line numbers of the selected element or elements, counting from                                 0. If nothing is selected, returns an empty tuple.

3. delete(first, last = None) : It is used to delete the lines which exist in the given range.

4. get(first, last = None): It is used to get the list items that exist in the given range.

5. index(i) :  It is used to place the line with the specified index at the top of the widget.

6. insert(index, *elements) : It is used to insert the new lines with the specified number of elements before the                                                        specified index.

7. nearest(y) : It returns the index of the nearest line to the y coordinate of the Listbox widget.

8. see(index) : It is used to adjust the position of the listbox to make the lines specified by the index visible.

9. size() :  It returns the number of lines that are present in the Listbox widget.

10. xview() : This is used to make the widget horizontally scrollable.

11. xview_moveto(fraction) : It is used to make the listbox horizontally scrollable by the fraction of width of the                                                          longest line present in the listbox.

12. xview_scroll(number, what) : It is used to make the listbox horizontally scrollable by the number of characters                                                         specified.

13. yview() : It allows the Listbox to be vertically scrollable.

14. yview_moveto(fraction) : It is used to make the listbox vertically scrollable by the fraction of width of the                                                             longest line present in the listbox.

15. yview_scroll (number, what) : It is used to make the listbox vertically scrollable by the number of characters s                                                            Specified.






This Article is Contributed by Yogesh singh .
Share your suggestions and feedback in comment sections.

Thank you for visiting here ..


No comments

Powered by Blogger.