

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- 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?
- Disable the underlying window when a popup is created in Python TKinter
- How can I change the text of the Tkinter Listbox item?
- Default to and select the first item in Tkinter Listbox
- How to fully change the color of a Tkinter Listbox?
- How to clear a Tkinter ListBox?
- How to get the width of the Tkinter widget?
- Return the input of the Entry widget in Tkinter
- Function to close the window in Tkinter