Data Handling & GUI
Programming in Python
Data Handling, Visualization, File I/O,
Serialization, and Tkinter GUI Programming
Data Handling Overview
• • Data handling involves storing, processing, and
analyzing data.
• • Common data formats: CSV, JSON, XML.
• • Tools: Pandas, NumPy, Matplotlib for visualization.
Data Visualization
• • Helps understand patterns and trends in data.
• • Libraries: Matplotlib, Seaborn, Plotly.
• • Example using Matplotlib:
• import [Link] as plt
• [Link]([1, 2, 3], [4, 5, 6])
• [Link]()
File I/O in Python
• • Reading and writing files using open().
• • Example:
• with open('[Link]', 'w') as f:
• [Link]('Hello, world!')
• with open('[Link]', 'r') as f:
• print([Link]())
Data Serialization - CSV
• • CSV (Comma-Separated Values) is a common data
format.
Data Serialization - CSV
Writing a CSV File:
import csv
data = [ ['Name', 'Age', 'City'],
['John', 25, 'New York’],
['Alice', 30, 'Los Angeles’],
['Bob', 35, 'Chicago’]]
with open('[Link]', 'w’) as file:
writer = [Link](file)
[Link](data)
Data Serialization - CSV
Reading a CSV File:
import csv
with open('[Link]', 'r') as file:
reader = [Link](file)
for row in reader:
print(row)
Data Serialization - CSV
Using pandas Library:
The pandas library provides a more convenient way to
read and write CSV files:
Reading a CSV File with pandas:
import pandas as pd
df = pd.read_csv('[Link]’)
print(df)
Data Serialization - CSV
Writing a CSV File with pandas:
import pandas as pd
data = { 'Name': ['John', 'Alice', 'Bob'],
'Age': [25, 30, 35],
'City': ['New York', 'Los Angeles', 'Chicago’]}
df = [Link](data)
df.to_csv('[Link]', index=False)
Data Serialization - JSON
• • JSON (JavaScript Object Notation) stores data in key-
value format.
• • Example:
Writing a JSON File:
import json
data = { "name": "John", "age": 30, "city": "New
York"}
with open('[Link]', 'w') as file:
[Link](data, file)
Data Serialization - JSON
Reading a JSON File:
import json
with open('[Link]', 'r') as file:
data = [Link](file)
print(data)
Data Serialization - XML
• • XML (Extensible Markup Language) is used for
structured data.
Here's an example of reading and writing XML in Python
using the [Link] module:
Writing an XML File:
import [Link] as ET
root = [Link]("root")
person = [Link](root, "person")
Data Serialization - XML
name = [Link](person, "name")
[Link] = “Sri“
age = [Link](person, “age”)
[Link] = "30”
tree = [Link](root)
[Link]("[Link]")
Data Serialization - XML
Reading an XML File:
import [Link] as ET
tree = [Link]("[Link]")
root = [Link]()
for person in [Link]("person"):
name = [Link]("name").text
age = [Link]("age").text
print(f"Name: {name}, Age: {age}")
Data Serialization - XML
1. [Link]():
2. This function is used to create a new XML element.
3. [Link](): This function is used to create
a new subelement.
4. [Link](): This function is used to parse an
XML file.
5. [Link](): This method is used to write the
XML tree to a file.
GUI Programming Overview
• • GUI (Graphical User Interface) allows user
interactions.
• • Tkinter is the standard Python GUI toolkit.
• • Used for creating buttons, forms, and interactive
apps.
GUI Programming Overview
Python offers several libraries and frameworks for creating
graphical user interfaces (GUIs).
Popular GUI Libraries in Python:
1. Tkinter: A built-in Python library that provides a simple
way to create GUI applications. It's easy to use and
comes bundled with Python.
2. PyQt: A powerful and feature-rich library that provides
a comprehensive set of tools for creating GUI
applications. It's widely used in industry and academia.
GUI Programming Overview
3. wxPython: A cross-platform library that provides a
native-looking GUI on Windows, macOS, and Linux.
4. PySide: Another Python binding for the Qt
framework, similar to PyQt.
5. Kivy: An open-source library for creating multi-
touch enabled GUI applications.
GUI Programming Overview
Key Concepts:
1. Widgets: GUI components such as buttons, labels, text
boxes, and menus that users can interact with.
2. Event-driven programming: GUI applications
respond to user interactions, such as button clicks or
keyboard input, by triggering events that execute specific
code.
3. Layout management: GUI components are arranged
in a specific layout to create a visually appealing and
user-friendly interface.
GUI Programming Overview
• Benefits:
• 1. Easy to learn: Python's syntax and nature make it an
excellent language for beginners and experienced
developers alike.
• 2. Fast development: Python's extensive libraries and
frameworks enable rapid development of GUI
applications.
• 3. Cross-platform: Many Python GUI libraries are cross-
platform, allowing you to deploy your application on
multiple operating systems.
Introduction to Tkinter
• Tkinter is Python's de-facto standard GUI (Graphical
User Interface) package. It is a thin object-oriented layer
on top of Tcl/Tk. Tkinter provides a powerful and easy-
to-use interface for creating GUI applications.
[Link]("500x300") #width x height
frame = [Link](root, width=500, height=300, bg="blue")
[Link]()
Example:
• import tkinter as tk
• root = [Link]()
• [Link]()
Introduction to Tkinter
• Tkinter Widgets:
• Some common Tkinter widgets include:
• 1. Label: Displays text or images.
• 2. Button: A clickable button that can perform an action.
• 3. Entry: A single-line text box for user input.
• 4. Text: A multi-line text box for user input or display.
• 5. Frame: A container for other widgets.
Introduction to Tkinter
Tkinter Layout Managers:
Tkinter provides three layout managers:
1. pack(): A simple layout manager that arranges
widgets in a block format.
2. grid(): A more complex layout manager that
arranges widgets in a table format.
3. place(): A layout manager that allows you to place
widgets at specific coordinates.
Creating Windows & Widgets
• • Widgets: Label, Button, Entry, Frame, etc.
• • Example:
• label = [Link](root, text='Hello')
• [Link]()
Handling Events & User
Interactions
• • Bind functions to widgets for interaction.
• • Example:
• def on_click():
• print('Button Clicked!')
• button = [Link](root, text='Click Me',
command=on_click)
• [Link]()
Tables, Buttons, Text Entries
• • Entry widget for user input.
• • Example:
• entry = [Link](root)
• [Link]()
Checkbuttons & Radiobuttons
• • Used for multiple-choice options.
• • Example:
• chk = [Link](root, text='Check Me')
• [Link]()
Menus in Tkinter
• • Menus provide navigation options.
• • Example:
• menu = [Link](root)
• [Link](menu=menu)
Layout Managers (pack, grid, place)
• • pack(): Stacks widgets.
• • grid(): Organizes widgets in a table.
• • place(): Absolute positioning.
• • Example:
• [Link](row=0, column=0)
Conclusion
• • Python supports data handling and visualization.
• • File I/O and serialization manage data storage.
• • Tkinter allows easy GUI creation.
• • Layout managers help in widget positioning.