If you've got data in a CSV file and you want to use that data to populate a QComboBox dropdown in your PyQt or PySide application, you're in the right place. This is a common pattern: fetch or generate data externally, save it locally, and then load it into your GUI.
Let's walk through exactly how to do this.
Reading a CSV File in Python
Before we touch any Qt code, let's make sure we can read the CSV file. Python's built-in csv module makes this straightforward.
Say you have a CSV file called items.csv that looks like this:
Name,Category
Apples,Fruit
Bananas,Fruit
Carrots,Vegetable
Lettuce,Vegetable
Chicken,Meat
To read the "Name" column from this file, you can use:
import csv
def load_items_from_csv(filepath):
items = []
with open(filepath, "r") as f:
reader = csv.DictReader(f)
for row in reader:
items.append(row["Name"])
return items
Calling load_items_from_csv("items.csv") would return ['Apples', 'Bananas', 'Carrots', 'Lettuce', 'Chicken']. That's a plain Python list of strings — exactly what we need to fill a combo box.
Adding Items to a QComboBox
A QComboBox is the standard dropdown widget in Qt. You can add items to it one at a time with addItem(), or add a whole list at once with addItems(). Since we already have a list from our CSV file, addItems() is the way to go.
Here's a minimal working example:
import sys
import csv
from PyQt6.QtWidgets import QApplication, QComboBox, QVBoxLayout, QWidget
def load_items_from_csv(filepath):
items = []
with open(filepath, "r") as f:
reader = csv.DictReader(f)
for row in reader:
items.append(row["Name"])
return items
app = QApplication(sys.argv)
window = QWidget()
layout = QVBoxLayout()
combo = QComboBox()
items = load_items_from_csv("items.csv")
combo.addItems(items)
layout.addWidget(combo)
window.setLayout(layout)
window.show()
sys.exit(app.exec())
When you run this, you'll see a window with a dropdown containing all the names from your CSV file. That's all it takes.
If you're using PySide6 instead of PyQt6, the code is identical — just change the import line to from PySide6.QtWidgets import .... For more on the differences, see our guide on PyQt6 vs PySide6.
Working with Simpler CSV Files
Your CSV file might not have a header row. Maybe it's just a flat list of values, one per line:
Apples
Bananas
Carrots
Lettuce
Chicken
In that case, you don't need the csv module at all. You can read the file directly:
def load_items_from_csv(filepath):
with open(filepath, "r") as f:
items = [line.strip() for line in f if line.strip()]
return items
The line.strip() call removes any trailing newline characters or whitespace, and the if line.strip() check skips any blank lines.
Refreshing the ComboBox When the CSV Changes
If your CSV file gets updated periodically — for example, after syncing with Google Drive — you'll want to refresh the combo box contents. To do this, clear the existing items first and then add the new ones:
def refresh_combo(combo, filepath):
combo.clear()
items = load_items_from_csv(filepath)
combo.addItems(items)
You could call this function on a button press, on a timer, or after your API sync completes. Here's an example with a refresh button using signals and slots to connect the button click to the refresh function:
import sys
import csv
from PyQt6.QtWidgets import (
QApplication, QComboBox, QPushButton,
QVBoxLayout, QWidget
)
def load_items_from_csv(filepath):
items = []
with open(filepath, "r") as f:
reader = csv.DictReader(f)
for row in reader:
items.append(row["Name"])
return items
def refresh_combo():
combo.clear()
items = load_items_from_csv("items.csv")
combo.addItems(items)
app = QApplication(sys.argv)
window = QWidget()
layout = QVBoxLayout()
combo = QComboBox()
refresh_combo() # Load initial data
refresh_button = QPushButton("Refresh")
refresh_button.clicked.connect(refresh_combo)
layout.addWidget(combo)
layout.addWidget(refresh_button)
window.setLayout(layout)
window.show()
sys.exit(app.exec())
Now clicking "Refresh" reloads the CSV file and updates the dropdown.
Storing Extra Data with Each Item
Sometimes you want to display one thing in the combo box but store something else behind the scenes — like showing a product name but keeping track of its ID. QComboBox supports this through the addItem() method's second argument, which sets the user data for that item.
def load_items_with_data(filepath):
"""Returns a list of (display_text, data) tuples."""
items = []
with open(filepath, "r") as f:
reader = csv.DictReader(f)
for row in reader:
items.append((row["Name"], row["Category"]))
return items
# Add items with associated data
for name, category in load_items_with_data("items.csv"):
combo.addItem(name, category)
Later, you can retrieve the stored data for the currently selected item using currentData():
selected_category = combo.currentData()
print(selected_category) # e.g. "Fruit"
This is useful when you need to look something up or pass additional context based on the user's selection.
Handling Missing Files Gracefully
Since you're loading from an external file, it's worth adding a little safety net in case the file doesn't exist yet (maybe the API sync hasn't run):
import os
def load_items_from_csv(filepath):
if not os.path.exists(filepath):
return []
items = []
with open(filepath, "r") as f:
reader = csv.DictReader(f)
for row in reader:
items.append(row["Name"])
return items
This way your application won't crash if the file is missing — the combo box will simply be empty until data is available.
Using Qt Designer
If you're building your UI in Qt Designer, you can add a QComboBox to your form and give it an object name (for example, comboBox). Then, in your Python code after loading the .ui file, you access the combo box through that name and populate it the same way:
items = load_items_from_csv("items.csv")
self.comboBox.addItems(items)
There's no way to link a CSV file directly in Qt Designer — the Qt Resource System is designed for bundling static assets like images and icons into your application. For dynamic data like CSV files, you load the data in your Python code and add it to the widget programmatically, as shown above.
Summary
Populating a QComboBox from a CSV file is a three-step process:
- Read the CSV using Python's
csvmodule (or plain file reading for simple lists). - Add the items to the combo box using
addItems()for a list of strings, oraddItem()with a second argument if you need to attach extra data. - Refresh when needed by calling
clear()followed byaddItems()again.
This pattern works well for any situation where your dropdown data comes from an external source — whether that's a CSV file synced from Google Drive, a local database export, or any other data file your application generates.
Create GUI Applications with Python & Qt6 by Martin Fitzpatrick
(PySide6 Edition) The hands-on guide to making apps with Python — Over 15,000 copies sold!