Tkinter ListBox Tutorial
The Tkinter ListBox is used in the following format:
ListBox(master, options, ...)
The first argument to the Listbox( ) method is the parent object, indicating which parent object this listbox will be built inside.
The following are other commonly used options parameters within the Listbox( ) method.
(1) bg or background
: the background color.
(2) borderwidth or bd
: border width, default is two pixels.
(3) cursor
: the shape of the cursor when the mouse cursor is on the list box.
(4) fg or froeground
: font color.
(5) font
: the font shape.
(6) height
: height, in characters, default is 10.
(7) highlightcolor
: color of the list box when it gets focus.
(8) highlightthickness
: the thickness of the list box when it gets the focus.
(9) listvariable
: handle the content of the options in a variable way.
(10) relief
: the default is relief=FLAT, which can control the outer frame of the list box, the default is SUNKEN.
(11) selectbackground
: the background color of the selected string.
(12) selectmode
: you can decide how many options can be selected, and how mouse dragging affects the options.
-
BROWSE: this is the default value, we can select one option, if select an option while dragging the mouse, it will cause the last position of the option is the position of the selected item.
-
SINGLE: Only one option can be selected, it can be selected by clicking, and the selected item cannot be changed by dragging.
-
MULTIPLE: You can select multiple options, click the item to toggle whether to select the item.
-
EXTENDED: Click the first item and then drag and drop to the last item, you can select a series of options in this area. Click can select the first item, at this time, if you hold down the Shift key and click another item, you can select the interval items.
(13) width
: width, the unit is characters.
(14) xscrollcommand
: use the scroll bar in the x-axis.
(15) yscrollcommand
: use scrollbar in y-axis.
Tkinter ListBox Tutorial Example#1
Create listbox1, and then create listbox2 using character height 5.
from tkinter import *
root = Tk()
root.title("apidemos.com")
root.geometry("300x210")
lb1 = Listbox(root) # create listbox 1
lb1.pack(side=LEFT,padx=5,pady=10)
lb2 = Listbox(root,height=5,relief="raised") # create listbox 2
lb2.pack(anchor=N,side=LEFT,padx=5,pady=10)
root.mainloop()
Output: