Loading ...

šŸ How to Use Tkinter Listbox to Add and Print Selected Items in Python

šŸ How to Use Tkinter Listbox to Add and Print Selected Items in Python


If you’re learning Tkinter, Python’s standard GUI library, and want to build a fun and interactive app, this guide is for you! In this tutorial, you’ll learn how to:

  • Create a simple Tkinter interface
  • Dynamically add items to a Listbox
  • Select multiple items
  • Print only the selected ones to the console

Let’s get started! šŸ§‘ā€šŸ’»


šŸ“¦ What is Tkinter?

Tkinter is the default GUI (Graphical User Interface) toolkit for Python. It allows you to create windows, buttons, text boxes, and other widgets with minimal code. It’s great for building desktop apps and learning event-driven programming.


šŸŽÆ Project Goal

We’ll create a small GUI app that:

  • Displays a list of fruits in a Listbox
  • Lets the user add more items using an Entry field
  • Allows selecting multiple items
  • Prints only the selected items to the console

šŸ§‘ā€šŸ’» Complete Python Code

Here’s the full working code:

import tkinter as tk

# Create the main window
root = tk.Tk()
root.title("Fruit Selector")
root.geometry("300x300")  # Optional: Set window size

# Function to print selected items
def order():
    selected_indices = listBox.curselection()
    for i in selected_indices:
        print(listBox.get(i))

# Function to add a new item to the Listbox
def add():
    listBox.insert(tk.END, addData.get())
    addData.delete(0, tk.END)  # Clear input after adding

# Create widgets
listBox = tk.Listbox(root,
                     background="#f7ffde",
                     selectmode="multiple")
addData = tk.Entry(root, width=20)
submitBtn = tk.Button(root, text="Submit", command=add, width=16)
printBtn = tk.Button(root, text="Print", command=order, width=16)

# Pack widgets
listBox.pack(pady=10)
addData.pack(pady=5)
submitBtn.pack(pady=5)
printBtn.pack(pady=5)

# Add default items
listBox.insert(0, "Mango")
listBox.insert(1, "Banana")
listBox.insert(2, "Apple")
listBox.insert(3, "Guava")
listBox.insert(4, "Pineapple")

# Run the GUI loop
root.mainloop()

šŸ” Explanation

1. tk.Listbox(...)

Creates a list where users can select multiple items thanks to selectmode="multiple".

2. curselection()

Returns the index(es) of selected items.

3. get(index)

Fetches the actual value at the specified index.

4. Entry(...)

Lets users type new items to be added to the list.

5. insert(...)

Appends new data to the Listbox.


šŸš€ Output

When you run this code, you’ll see:

  • A list of fruits
  • An input box to type new items
  • A Submit button to add them
  • A Print button that prints only the selected items in the terminal

āœ… Easy and fun for beginners learning GUI development in Python!


šŸ’” Use Cases

  • Grocery item selectors
  • Quiz question selectors
  • Task list apps
  • Any app needing dynamic selection

šŸ“ˆ SEO & Learning Benefits

This simple Tkinter Listbox app helps you understand:

  • Event handling
  • Widget layout
  • User input management
  • Dynamic interface updates

Target Keywords:

  • Tkinter Listbox add items
  • Tkinter print selected items
  • Python GUI projects for beginners
  • Python Listbox tutorial

šŸ”š Conclusion

The Tkinter Listbox is a powerful widget that makes user selection in GUIs a breeze. With just a few lines of Python code, you can build dynamic apps with real-time interaction. This project is great for students, hobbyists, and anyone looking to strengthen their Python GUI skills.


✨ Like this tutorial?

āœ… Share it
āœ… Bookmark it
āœ… Comment below with your own Listbox variations!


Let me know if you want a downloadable .zip of the code or if you’d like this styled in a WordPress block format (with HTML/shortcodes) for direct copy-paste!

Related Posts

Building a Modern CRUD Menu Application with Python and ttkbootstrap

Title: Building a Modern CRUD Menu Application with Python and ttkbootstrap Introduction Welcome to my latest tech adventure! In this blog post, I’ll walk you through the development of a…

Read more

Building Car Racing Elite: A High-Graphics Pygame without any images

Want to create a thrilling car racing game with stunning visuals using Python? In this step-by-step Pygame tutorial, I’ll guide you through building “Car Racing Elite”—a modern racing game with…

Read more

Building a Modern Menu Viewer with Python and Tkinter: A Step-by-Step Guide

Morden Menu Viewer with Python Looking to build a modern desktop application with Python? In this detailed guide, I’ll walk you through creating a sleek menu viewer using Python’s Tkinter…

Read more

šŸŽÆ Title: Build a Modern Calculator Using Python Tkinter

Here’s a complete blog post explaining how to build a modern calculator using Python Tkinter from scratch. This will cover everything from setting up Tkinter to customizing the UI for…

Read more

Jumping Jack Game with Python and Pygame

Let’s build a simple Jumping Jack game using Pygame, a popular library for game development in Python. This game will feature basic mechanics like jumping, gravity, and obstacle dodging, with…

Read more

Flappy Bird clone using Pygame

Let’s build a simple Flappy Bird clone using Pygame, a popular library for game development in Python. Here’s a basic structure for the game: Step 1: Install Pygame Make sure…

Read more

Leave a Reply

Your email address will not be published. Required fields are marked *