How To Add A Listbox In Tkinter In Python

Introduction

 
In this article, I will explain how to add a listbox in Tkinter in Python.
 
Definition
 
The Listbox keyword is used to display a list of items from which a user can select a number of items in the listbox.
 
Syntax
  1. w = Listbox( a, option)  
Parameters
 
a − This represents the parent window of the computer.
option − The list of most used options for this widget.
 
Option & Description
  • bd - The bd is used to change the Border width in pixels. The default is 2.
  • bg - The bg is used to change the Normal background color background.
  • fg - The fg is used to change the normal foreground (text) color.
  • font - The font is used to change the text font .
  • Cursor - The cursor is used to set this option to a cursor name, the mouse cursor will change to that pattern when it is over the listbox.
Program
  1. from tkinter import *        
  2. a= Tk()  
  3. a.geometry("400x400")  
  4. a.title("test")  
  5. Lb1 = Listbox(a,bg = "pink", bd = 10, fg = "black",\  
  6.               font = "Castellar", cursor = "target")  
  7. Lb1.insert(1"lion")  
  8. Lb1.insert(2"tiger")  
  9. Lb1.insert(3"zebra")  
  10. Lb1.insert(4"elephant")  
  11. Lb1.insert(5"deer")  
  12. Lb1.insert(6"fox")  
  13. Lb1.insert(7"Wolf")  
  14. Lb1.insert(8"Gorilla")  
  15. Lb1.insert(9"Jackal")  
  16. Lb1.insert(10"Otter")  
  17. Lb1.pack()  
  18. a.mainloop()  
Output
 
How To Add  Listbox In Tkinter In Python
  • HighlightBackground - we used to focus highlight when the frame does not have a focus
  • HighlightColor - we used to show the focus highlight when the frame has a focus.
  • HighlightThickness - we used the thickness of the focus highlight.
  • Select Background - we used background color to use displaying selected text.
Program
  1. from tkinter import *        
  2. a= Tk()  
  3. a.geometry("400x400")  
  4. a.title("test")  
  5. listbox1=Listbox(a, background="yellow", fg="black",  
  6.                  highlightbackground="blue",highlightthickness=10,  
  7.                  selectbackground="green",highlightcolor="red")  
  8. listbox1.grid(row=1, column=1)  
  9. for x in range(110):  
  10.     listbox1.insert(END, x)  
  11. a.mainloop()  
Output
 
How To Add  Listbox In Tkinter In Python
  • height - The height is used to change the height on the listbox in text lines (text buttons) or pixels (images).
  • width - The width is used to change the width on the listbox in letters.
  • Justify - The justify is used to change the arrangement order LEFT, CENTER, RIGHT.
  • Xscrollcommand - The xscrollcommand keyword used for the canvas is scrollable, this attribute must be the. set () method of the horizontal scrollbar.
  • Yscrollcommand - The yscrollcommand keyword used for the canvas is scrollable, this attribute must be the. set () method of the vertical scrollbar.
Program
  1. from tkinter import *    
  2. a=Tk()    
  3. a.title("scrollbar")    
  4. list_one=Listbox(a,width=20,height=20,  
  5.                  justify=CENTER,bg="orange",bd=20)    
  6. scroll_one=Scrollbar(a,command=list_one.yview)    
  7. list_one.configure(yscrollcommand=scroll_one.set)    
  8. list_one.pack(side=LEFT)    
  9. scroll_one.pack(side=RIGHT,fill=Y)    
  10. for i in range(100):    
  11.     list_one.insert(END,i)    
  12. a.mainloop()  
Output
 
How To Add  Listbox In Tkinter In Python
 

Conclusion

 
In this article, we have seen how to add a listbox in Tkinter in Python. I hope this article was useful to you. Thanks for reading!


Recommended Free Ebook
Similar Articles