- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to fit Tkinter listbox to contents?
Tkinter offers Listbox widgets which is very useful in the case of representing a large set of data items in the form of a list. To configure the listbox widget, we can use configure(*options) method to change the properties such as background color, foreground color, and other properties of the listbox widget. The width property is used to define the width of the listbox widget. If we set width=0, then it will get closed to the length of its content in the listbox.
Example
# Import the required libraries from tkinter import * # Create an instance of tkinter frame or window win=Tk() # Set the size of the window win.geometry("700x350") # Add a Listbox widget with number as the list items listbox =Listbox(win) listbox.insert(END,"C++", "Java", "Python", "Rust", "GoLang", "Ruby", "JavaScript", "C# ", "SQL", "Dart") listbox.pack(side=LEFT, fill=BOTH) # Configure the Listbox widget to set the width to its content listbox.configure(background="skyblue4", foreground="white", font=('Aerial 13'), width=0) win.mainloop()
Output
Now, run the above code to display the listbox which is set to its content.
- Related Articles
- How to stop Tkinter Frame from shrinking to fit its contents?
- How to clear a Tkinter ListBox?
- How to edit a Listbox item in Tkinter?
- How to keep selections highlighted in a Tkinter Listbox?
- How to display a Listbox with columns using Tkinter?
- How to fully change the color of a Tkinter Listbox?
- How to remove multiple selected items in the listbox in Tkinter?
- How to select at the same time from two Tkinter Listbox?
- How to directly modify a specific item in a TKinter listbox?
- Attach scrollbar to listbox as opposed to window in Tkinter
- How to Delete Tkinter Text Box's Contents?
- Default to and select the first item in Tkinter Listbox
- How to clear the contents of a Tkinter Text widget?
- How to save the contents of a Textbox in Tkinter?
- Creating scrollable Listbox within a grid using Tkinter

Advertisements