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
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
- from tkinter import *
- a= Tk()
- a.geometry("400x400")
- a.title("test")
- Lb1 = Listbox(a,bg = "pink", bd = 10, fg = "black",\
- font = "Castellar", cursor = "target")
- Lb1.insert(1, "lion")
- Lb1.insert(2, "tiger")
- Lb1.insert(3, "zebra")
- Lb1.insert(4, "elephant")
- Lb1.insert(5, "deer")
- Lb1.insert(6, "fox")
- Lb1.insert(7, "Wolf")
- Lb1.insert(8, "Gorilla")
- Lb1.insert(9, "Jackal")
- Lb1.insert(10, "Otter")
- Lb1.pack()
- a.mainloop()
Output
-
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
- from tkinter import *
- a= Tk()
- a.geometry("400x400")
- a.title("test")
- listbox1=Listbox(a, background="yellow", fg="black",
- highlightbackground="blue",highlightthickness=10,
- selectbackground="green",highlightcolor="red")
- listbox1.grid(row=1, column=1)
- for x in range(1, 10):
- listbox1.insert(END, x)
- a.mainloop()
Output
-
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
- from tkinter import *
- a=Tk()
- a.title("scrollbar")
- list_one=Listbox(a,width=20,height=20,
- justify=CENTER,bg="orange",bd=20)
- scroll_one=Scrollbar(a,command=list_one.yview)
- list_one.configure(yscrollcommand=scroll_one.set)
- list_one.pack(side=LEFT)
- scroll_one.pack(side=RIGHT,fill=Y)
- for i in range(100):
- list_one.insert(END,i)
- a.mainloop()
Output
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!