Tkinter Listbox widgets are used to display scrollable boxes with vertically stacked menus. Within the window, the user can select either one or multiple items from the widget. In Tkinter, all the widgets are aligned either vertically or horizontally, and sometimes it seems difficult to arrange the widget position whenever we resize our window.
We can configure the Listbox widget property by using expand=True and fill=BOTH property. These properties ensure that the widget stretches both vertically and horizontally. However, expand allows the widget to grow in available space.
Example
#Import tkinter library from tkinter import * #Create an instance of Tkinter frame or window win= Tk() #Set the geometry of tkinter frame win.geometry("750x250") listbox=Listbox(win) #Create a listbox widget listbox.pack(padx=10,pady=10,fill=BOTH, expand=True) #fill=BOTH stretch the widget both vertically and horizontally #expand=True, expand the widget in the available space listbox.insert(1, "Python") listbox.insert(2, "Java") listbox.insert(3, "C++") listbox.insert(4, "Rust") listbox.insert(5, "GoLang") listbox.insert(6, "C#") listbox.insert(7, "JavaScript") listbox.insert(8, "R") listbox.insert(9, "Php") win.mainloop()
Output
Running the above code will display a list of programming languages.
When we resize the window, the Listbox will maintain its width and height with respect to the window.