0% found this document useful (0 votes)
5 views

DVA Assignment 4

The document discusses the role of Tkinter in Python for creating GUI applications, highlighting its simplicity, built-in features, and popular widgets. It covers layout management techniques, menu and dialog creation, and the integration of the sqlite3 module for database operations. Additionally, it provides code examples for inserting, displaying, and deleting records in a Tkinter application connected to an SQLite database.

Uploaded by

Tanya Maheshwari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

DVA Assignment 4

The document discusses the role of Tkinter in Python for creating GUI applications, highlighting its simplicity, built-in features, and popular widgets. It covers layout management techniques, menu and dialog creation, and the integration of the sqlite3 module for database operations. Additionally, it provides code examples for inserting, displaying, and deleting records in a Tkinter application connected to an SQLite database.

Uploaded by

Tanya Maheshwari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

DVA Assignment 4

Name: Tanya Maheshwari


Enrollment No. 02613702022
Submitted to: Ms. Kavita Srivastva
1. Role of Tkinter in Python for GUI Applications (CO4)
Answer:
Tkinter is the standard GUI (Graphical User Interface) library included with
Python. It provides tools to create windows, dialogs, buttons, text fields, and
other GUI elements. Tkinter uses the Tcl/Tk GUI toolkit under the hood and is
known for its simplicity and ease of use, especially for beginners.
Advantages:
 Built-in: No installation required.
 Cross-platform: Works on Windows, macOS, and Linux.
 Lightweight: Ideal for small-to-medium GUI apps.
 Well-documented: Large community and support.
Popular Tkinter widgets:
 Label: Displays text or images.
 Button: Triggers a function on click.
 Entry: Single-line text input.
 Text: Multi-line text area.
 Listbox: Displays a list of selectable items.
 Checkbutton, Radiobutton: Used for choices.
 Frame: Container for organizing widgets.
Tkinter remains a top choice for prototyping, educational tools, and lightweight
applications due to its simplicity and integration with Python.
2. Layout Management in Tkinter: pack(), grid(), place()
Answer:
Tkinter offers three layout managers to organize widgets:
1. pack():
o Arranges widgets in blocks (top, bottom, left, right).

o Simple and automatic sizing.

o Best for basic or vertically stacked layouts.

2. grid():
o Organizes widgets in rows and columns (table-like).

o More control than pack().

o Ideal for forms and structured layouts.

3. place():
o Uses absolute positioning (x, y coordinates).

o Offers maximum control but hard to maintain.

o Best used when precise control over placement is needed.

Comparison:

Meth Ease of Best Use


Flexibility
od Use Case

Simple
pack() High Low
layouts

Medium- Structured
grid() Medium
High forms

Custom
place() Low High
layouts

3. Menus and Dialog Boxes in Tkinter


Answer:
Creating Menus:
Tkinter uses the Menu widget to add menu bars and dropdowns.
python
CopyEdit
menu = Menu(root)
root.config(menu=menu)
file_menu = Menu(menu)
menu.add_cascade(label="File", menu=file_menu)
file_menu.add_command(label="Open")
Creating Dialog Boxes:
Tkinter provides built-in dialog boxes via the tkinter.messagebox and
tkinter.simpledialog modules.
Types of Dialogs:
 Message Boxes:
o showinfo(), showwarning(), showerror(): Used for feedback.
o askyesno(), askquestion(): For user confirmation.

 Input Dialogs:
o simpledialog.askstring(), askinteger(): Prompt user for input.

Example:
python
CopyEdit
from tkinter import messagebox
messagebox.showinfo("Success", "Record added successfully!")
Menus and dialog boxes enhance interactivity, usability, and professionalism in
applications.

4. Role of sqlite3 Module in Python for Database Operations


Answer:
The sqlite3 module is Python’s built-in interface for SQLite, a lightweight, disk-
based database. It is ideal for small-scale applications and does not require a
separate server process.
CRUD Operations:
 Create: INSERT INTO statements add data.
 Read: SELECT queries retrieve records.
 Update: UPDATE modifies data.
 Delete: DELETE FROM removes records.
Advantages:
 Lightweight and fast.
 Easy integration with Python.
 Self-contained database (single file).
 Ideal for prototyping, education, and embedded systems.
Example:
python
CopyEdit
import sqlite3
conn = sqlite3.connect('mydb.db')
cursor = conn.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS Students (ID INTEGER PRIMARY
KEY, Name TEXT, Grade TEXT)")
conn.commit()
conn.close()
sqlite3 is a key tool for developing data-driven desktop apps using Python and
Tkinter.

5. Steps to Integrate Database with Tkinter Application


Answer:
To integrate a database with Tkinter:
1. Connect to Database:
python
CopyEdit
import sqlite3
conn = sqlite3.connect('students.db')
cursor = conn.cursor()
2. Create Table (if not exists):
python
CopyEdit
cursor.execute('''CREATE TABLE IF NOT EXISTS Students (ID INTEGER PRIMARY
KEY, Name TEXT, Grade TEXT)''')
3. Insert Record:
python
CopyEdit
cursor.execute("INSERT INTO Students (Name, Grade) VALUES (?, ?)", (name,
grade))
conn.commit()
4. Retrieve Records:
python
CopyEdit
cursor.execute("SELECT * FROM Students")
records = cursor.fetchall()
5. Delete Record:
python
CopyEdit
cursor.execute("DELETE FROM Students WHERE ID=?", (student_id,))
conn.commit()
6. Close Connection:
python
CopyEdit
conn.close()
All these operations can be triggered by Tkinter buttons, entries, and
listboxes, creating a fully interactive GUI-based CRUD system.

6. Tkinter Application to Insert Records into SQLite


Answer (Code Example):
python
CopyEdit
import tkinter as tk
from tkinter import messagebox
import sqlite3

def insert_record():
name = entry_name.get()
grade = entry_grade.get()
conn = sqlite3.connect("students.db")
cursor = conn.cursor()
cursor.execute("INSERT INTO Students (Name, Grade) VALUES (?, ?)", (name,
grade))
conn.commit()
conn.close()
messagebox.showinfo("Success", "Student record inserted.")

# GUI
root = tk.Tk()
root.title("Student Entry")

tk.Label(root, text="Name").pack()
entry_name = tk.Entry(root)
entry_name.pack()

tk.Label(root, text="Grade").pack()
entry_grade = tk.Entry(root)
entry_grade.pack()

tk.Button(root, text="Insert", command=insert_record).pack()

root.mainloop()
This app allows user input for Name and Grade, inserts the data into the
“Students” table, and displays a confirmation on success.
7. Tkinter Interface to Display and Delete Records
Answer (Code Example):
python
CopyEdit
import tkinter as tk
from tkinter import messagebox
import sqlite3

def load_records():
listbox.delete(0, tk.END)
conn = sqlite3.connect("students.db")
cursor = conn.cursor()
cursor.execute("SELECT * FROM Students")
for row in cursor.fetchall():
listbox.insert(tk.END, f"{row[0]} - {row[1]} - {row[2]}")
conn.close()

def delete_record():
selected = listbox.get(tk.ACTIVE)
if selected:
student_id = selected.split(" - ")[0]
conn = sqlite3.connect("students.db")
cursor = conn.cursor()
cursor.execute("DELETE FROM Students WHERE ID=?", (student_id,))
conn.commit()
conn.close()
load_records()
messagebox.showinfo("Deleted", "Record deleted successfully.")

# GUI
root = tk.Tk()
root.title("View & Delete Students")

listbox = tk.Listbox(root, width=50)


listbox.pack()

tk.Button(root, text="Delete Record", command=delete_record).pack()


tk.Button(root, text="Refresh", command=load_records).pack()

load_records()
root.mainloop()

You might also like