
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Resize the Tkinter Listbox widget when the window resizes
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.
- Related Articles
- Dynamically Resize Buttons When Resizing a Window using Tkinter
- How can I resize the root window in Tkinter?
- How to resize the background image to window size in Tkinter?
- Attach scrollbar to listbox as opposed to window in Tkinter
- Getting every child widget of a Tkinter window
- Is it possible to color a specific item in a Tkinter Listbox widget?
- Python Tkinter – How to position a topLevel() widget relative to the root window?
- How to clear a Tkinter ListBox?
- Disable the underlying window when a popup is created in Python TKinter
- How can I change the text of the Tkinter Listbox item?
- How to fully change the color of a Tkinter Listbox?
- Default to and select the first item in Tkinter Listbox
- How to fit Tkinter listbox to contents?
- How to Resize Browser Window in WebDriver?
- How to remove multiple selected items in the listbox in Tkinter?
