Python Tkinter : How to export data from Entry Fields to a CSV file?
Python is a powerful programming language and is preferred for developing a wide range of applications. It is highly popular among developers due to its simplicity, readability, and ease of use. Tkinter is one of the most widely used GUI packages for Python. Tkinter provides several widgets that developers can use to build visually appealing applications. In this article, we will discuss how to export data from Entry Fields to a CSV file using Tkinter.
What is CSV?
CSV stands for “Comma-Separated Values.” It is a simple way of storing data in a tabular format wherein each row represents a record, and each column represents a field. Each field is separated by a comma, which is why it’s called a Comma-Separated Value file. A CSV file can be read and written using several applications, including Excel, Google Sheets, and Notepad++, among others.
The Tools
To export data from Entry Fields to a CSV file, we will use the following tools:
- Python 3.x: It is the latest stable version of Python. It can be downloaded from the official Python website (https://www.python.org/downloads/).
-
Tkinter: It is included with Python. Therefore, no additional installation is required.
-
Pandas: It is a data manipulation library. It provides several functions to read and write data in various file formats, including CSV. Install pandas by running the following pip command:
pip install pandas
The Code
The following code snippet will create a GUI application using Tkinter, which will contain two Entry Fields and a “Export to CSV” button. Upon clicking the “Export to CSV” button, the data entered in the Entry Fields will be exported to a CSV file:
import tkinter as tk
import pandas as pd
class Exporter:
def __init__(self, parent):
self.parent = parent
self.create_widgets()
def create_widgets(self):
tk.Label(self.parent, text="First Name:").grid(row=0, column=0)
self.first_name_entry = tk.Entry(self.parent)
self.first_name_entry.grid(row=0, column=1)
tk.Label(self.parent, text="Last Name:").grid(row=1, column=0)
self.last_name_entry = tk.Entry(self.parent)
self.last_name_entry.grid(row=1, column=1)
tk.Button(self.parent, text="Export to CSV", command=self.export_to_csv).grid(row=2, column=0, columnspan=2)
def export_to_csv(self):
data = {"First Name": [self.first_name_entry.get()],
"Last Name": [self.last_name_entry.get()]}
df = pd.DataFrame(data)
df.to_csv("export.csv", index=False)
if __name__ == "__main__":
root = tk.Tk()
app = Exporter(root)
root.mainloop()
The Explanation
Let’s break down the code to understand how it works:
- We start by importing the required libraries – Tkinter and pandas.
-
We create a class called “Exporter,” which will hold the GUI logic.
-
In the
__init__
method, we setself.parent
to the passed argument and call thecreate_widgets
method. -
In the
create_widgets
method, we create two Label widgets to display the text “First Name” and “Last Name.” The Entry widgets are then created and placed next to the corresponding Label widget using thegrid
function. -
Next, we create a Button widget labeled “Export to CSV,” which, when clicked, will invoke the
export_to_csv
method. -
In the
export_to_csv
method, we create a dictionary calleddata
, which will hold the data entered in the Entry Fields. We pass the data dictionary to the DataFrame constructor to create a pandas DataFrame. Finally, we call theto_csv
method to export the DataFrame to a CSV file named “export.csv.” Theindex=False
argument tells pandas not to write the DataFrame index to the CSV file. -
In the
if __name__ == "__main__":
block, we create an instance of the Exporter class, setroot
as the parent, and call themainloop
method to run the GUI application.
The Output
Enter some data in the Entry Fields and click the “Export to CSV” button. A CSV file named “export.csv” will be created in the current working directory, which will contain the data entered in the Entry Fields:
First Name,Last Name
John,Doe
Conclusion
Exporting data from Entry Fields to a CSV file is a common requirement in GUI applications. Python provides several libraries that make this task easy to accomplish. Tkinter is a powerful GUI package that provides several widgets to build visually appealing applications, and pandas is a versatile data manipulation library that provides several functions to read and write data in various file formats, including CSV. By combining Tkinter and pandas, we can easily export data from Entry Fields to a CSV file.
In this article, we have discussed how to create a GUI application using Tkinter that contains two Entry Fields and a “Export to CSV” button. Upon clicking the “Export to CSV” button, the data entered in the Entry Fields is exported to a CSV file using pandas. We hope this article has been informative and useful to you.