The document provides an overview of creating Graphical User Interfaces (GUIs) in Python using the Tkinter library, detailing various widgets like buttons, labels, and text boxes. It also introduces SQLite as a lightweight database engine and explains how to use the sqlite3 module in Python for database operations such as creating tables, inserting, selecting, updating, and deleting records. Additionally, it covers layout management techniques in Tkinter, including pack, grid, and place methods.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0 ratings0% found this document useful (0 votes)
3 views12 pages
Unit V-GUI - SQL2023-24
The document provides an overview of creating Graphical User Interfaces (GUIs) in Python using the Tkinter library, detailing various widgets like buttons, labels, and text boxes. It also introduces SQLite as a lightweight database engine and explains how to use the sqlite3 module in Python for database operations such as creating tables, inserting, selecting, updating, and deleting records. Additionally, it covers layout management techniques in Tkinter, including pack, grid, and place methods.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 12
UNIT-V
GU Interface:
GUI stands for Graphical User Interface. In Python, GUI is a way to create visual interfaces for users to
interact with the program. It allows the user to interact with the program by using graphical elements
al.
such as buttons, menus, text boxes, etc. instead of typing commands in the tei
GUI (Graphical User Interface) is a way to provide a visual interface
In Python, there are several libraries available to create GUIs. One of the most popular is Tkinter, whit
or users (0 interact with the program.
h
comes pre-installed with Python,
‘The tkinter Modul
‘Tkinter is a built-in module in Python that provides a way to create GUI (Graphical User Interface)
applications. It is the standard Python interface to the Tk GUI toolkit, which is a cross-platform toolkit
that is widely used for building desktop applications.
Features of the Tkinter module:
% It provides a set of widgets, such as buttons, labels, text boxes, and frames, to create GUIs,
v
It has a powerful layout manager that allows you to place widgets in the window or frame.
a
It provides event handling mechan
m, where you can bind an event, such as a button click, to
a function.
> It supports standard dialogs such as file open dialog, message boxes, etc. Ithas a simple and easy-to~
Jearn APL.
Window and Widgets: "window" typically refers to the top-level container for a graphical user
interface (GUI) application
Iget" rel ny graphical elem
nt inside the window, such as buttons, text boxes, labels, ete.
Window(Top level Containers): "window" typically refers to the top-level container for a
graphical user interface (GUI) application,
Exampl
import tkinter as tk
# create a new window
window = tk.TkQ,
window.title(" My First GUI");
# run the main loop
window.mainloop()
Output:
# MyFnnct - o x
BSc V - Python Programming(NEP) -2023-24 Page 1Some of the window and widgets of Tkinter
‘Top Level Containers Usage
Tk ‘This is the main window or root window of the
application, It is the fi
« widget that should be created
in any Tkinter program.
Top level This is a secondary window or dialog box that can be
created using the Tkinter Toplevel widget. It can be
used to display additional information or to prompt
the user for input.
Frame This is a container widget that can be used to group,
other widgets together. It can be used to organize the
layout of the application,
Paned Window: ‘This is a container widget that can be used to create a
resizable pane with multiple sub-panes. It can be used
to create a split view or a resizable layout,
Widgets(Low level components): In Tkinter, widgets are the low-level components
used to build the graphical user interface (GUD. Here are some of the most commonly used
widgets in Tkinter:
1. Label: This widget is used to display text or images.
2. Buttos
this widget is used to trigger an action when clicked,
. Entry: This widget is used to get user input through a single line text box,
|. Text: This widget is used to display or edit multi-line text.
. Checkbutton: This widget is used to toggle a boolean value.
1. Listbox: This widget is used to display a list of items from which the user ean select one or more
3.
4
8
6. Radiobutton: This widget is used to select a single option from a group of options.
1
8
Seale: Thi
widget is used to sel
ta value from a range of values by
slider.
9. Canvas: This widget is used to draw graphics, shapes, and images.
10. Menu: This widget is used to create menus and sub-menus.
aging a
1. Label Widget: The Label widget in Tkinter is used to display text or images on the GUI. It is a
simple widget that can be created using the Label class from the tkinter module.
Ex urput:
import tkinter as tk
root tk.TkO, ’
label=tk.Label(root, text." Hello World!")
label. pack()
root.mainloop()
BSc V - Python Programming(NEP) -2023-24
Page 22. Button Widget: The Button widget in Tkinter is used to create a clickable button on the GUL. It
is a simple widget that can be created using the Button class from the tkinter module.
Ex:
import tkinter as tk
def button_elick0):
rint("Button clicked")
ro0t = tk.TkO
button = tk.Button(root, text="Click Mel", command=button_click)
button. pack()
root.mainloop()
output:
3. Entry Widget: The Entry widget in Tkinter is used to create a single-line text box where the
Jing the Entry class from
user can enter text or values. It is a simple widget that can be created
the tkinter module,
Ex:
import tkinter as tk
def submit:
value = entry.get
print("User entered:", value)
root = tk.TkO)
entry = tk.Entry(root)
centry.packO
submit_button = tk.Button(root, text="Submit", command=submit)
submit_button.packO)
root.mainloop()
Output:
BSc V - Python Programming(NEP) -2023-24 Page 34. Text widget: The Text widget in Tkinter is used to create a mufti-line text box where the user
can enter or display multiple lines of text. It is a versatile widget that can be created using the
Text class from the tkinter module
Ex:
import tkinter as tk
root = tk.TkO,
text = tk.Text(root)
text.pack(),
textinsert((k.END, ‘This is a multi-line text widget.)
root.mainloop()
Output:
tu = 8 %
5. Cheekbutton Widget: The Checkbutton widget in Tkinter is used to create a checkbox that
can be selected or deselected by the user. It is a simple widget that can be created using the
Checkbutton class from the tkinter module.
import tkinter as tk
tk.TkO
varl = tk. Boolean Var() var2 = tk.BooleanVar()
ekbl = tk.Checkbutton(root, text="Pthon", variable=varl)
s=var2)
root
ckb2 = tk.Checkbutton(root, ext="Java', variabl
ckbl.pack()
ckb2,puck()
root.maintoop()
Output: >
- fe - ao xX
FF Pthon
F Joe
6. Radiobutton Widget: The Radiobutton widget in Tkinter is used to create a group of radio
buttons where only one option can be selected at a time. It is a simple widget that can be created
using the Radiobutton class from the tkinter module.
BSc V - Python Programming(NEP) -2023-24 Page 4import tkinter as tk
root = tk.TkO)
var = tk.StringVar(value="Option 1")
r1= tk,Radiobutton(root, text
‘Male", variable=var, value="Male")
FL.pack0
12 = tk.Radiobutton(root, text="Female", variable=var, value~"Female”)
12.packO,
root.mainloop()
Output:
7. Listbox Widget: The Listbox widget in Tkinter is used to display a list of items from which the user
can select one or more items. It is a versatile widget that can be created using the Listbox class
from the tkinter module,
Ex:
import tkinter as tk
root = tk. TKO,
Ib = tk.Listbox(root)
Ib.pack)
Ib.insert(1, "India")
Ib.insert(2, "USA")
Ib.insert(3, "China")
root.mainloop()
Output:
BSc V - Python Programming(NEP) -2023-24 Page 58. Seale Widget: The Scale widget in Tkinter is used to create a slider that allows the user to select a
value from a range of values. It is a simple widget that can be created using the Scale class from
the tkinter module.
Ex:
import tkinter as tk
root = tk. TKO,
scale = tk,Scale(root, from_=0, t0=100, orient=tk. HORIZONTAL)
seale.pack()
root.mainloopO
Output:
9. Canyas Widget: The Canvas widget in Tkinter is a versatile widget that allows you to create
and manipulate 2D graphics. It provides a virtual canvas on which you can draw various shapes
and images, as well as add text and other widgets.
import tkinter as tk
ro0t = tk.TkO)
canvas = tk.Canvas(root, width=200, height=200)
canvas. packO
canvas.create_rectangle(50, 50, 150, 150, fill="red")
root.mainloop()
Output:
10. Menu Widget: The Menu widget in Tkinter provides a way to create menus in your GUL. It
rarchical structure of menu items, with each item either iggering a
allows you to create a
command or displaying a submenu.
Ex:
import tkinter as tk
root = tk. TKO,
BSc V - Python Programming(NEP) -2023-24 Page 6menu_bar = tk.Menu(root)
file menu = tk.Menu(nnenu_bar, tearoff=0)
file_menu.add_command(labe!
file menu.add_cornmand(label
file_menu.add_separatorO,
file menu.add_command(label="Exit", command=root.quit)
menu_bar.add_cascade( label
root.config(menu=menu_bar)
root.mainloop))
Output:
Layout Management- pack, grid and place.
In Tkinter, there are three layout managers that can be used to arrange the widgets on the screen: pack,
grid, and place.
1. pack: The pack layout manager places widgets in the available space in a horizontal or vertical
direction. It is the simplest of the three managers and is often used for s
method takes several options,
mple interfaces. The pack
has side to specify the side of the parent widget to pack the child
widget and fill to specify how the widget should expand to fill available space,
x:
import tkinter as tk
root = tk.TkO,
root geometry("200x200")
# Using pack layout manager
labell =tklabel(root, text="Enter your name:
label L_pack(side="
entryi = tk.Entry(ro0)
entryl_pack(side="left")
buttonl
tk Button(root, te:
ick (si
buttonl,
root.mainloop),
BSc V - Python Programming(NEP) -2023-24 Page 7a |
2. grid: The grid layout manager arranges widgets in a grid pattern with rows and columns. You can specify
the number of rows and columns and then plaice the widgets in the cells using the grid method. The grid
method takes several options, such as row and column to specify the cell to place the widget and sticky to
specify how the widget should expand to fill the cell
x:
import tkinter as tk
root = tk. TKO,
root. geometry("" 300)(300")
# Using grid layout manager
label2= tk.Label(root, text="Enter your age:
label2.grid(row=0, column=0)
centry2 = tk-Entry(root)
entry2.grid(row=0, column=1)
button? = tk.Button(root, te:
button2.grid(row=1, column=0, columnspan:
root.mainloop()
Output:
3. place: The place layout manager allows you to speci the exact location of a widget using x and y
coordinates. You can also use relative positioning and offsets to position the widget. The place
method takes several options, such as x and y to specify the coordinates of the widget and relx
and rely to specify the relative position of the widget.
BSc V - Python Programming(NEP) -2023-24 Page 8import tkinter as tk
root = tieTk)
root.zeometry("300x 100")
# Using place layout manager
label3=tklabel(root, tex
labe 13.place(x=10, y=70)
Enter your email:")
entry3 = tk Entry(root)
entry3.place(x=100, y=70)
button3 = tk Button(root, text="Subrnit")
button3.place(x=220, y=65)
root.mainloopQ,
Output:
Python SQLite:
SQLite is a lightweight,
Python,
erverless, and self-contained SQL database engine that is widely used with
SQLite is a software library that provides a relational database management system (RDBMS) for
embedded and small-scale applications. ‘It is a_ self-contained, —_serverless, _zero-
and transa
configuration, jonal SQL database engine that is widely used in various applications such
as mobile devices, desktop applications, and web browsers,
SQLite is designed to be fast, lightweight, and easy to use, with a small memory footprint and low CPU
usage. It stores data in a single file, making it easy to distribute and manage, and supports many standard
SQL features, such as transactions, indexes, triggers, and views.
SQLite is a popular choice for developers who need a local database solution that is easy to set up and
use, and does not require a dedi
-ated server or complex administration. It is also used as a testing and,
development database, and 3
5 a data storage format for many applications and operating systems.
Light-weight RDBMS software to manage data in file format.
Download link: https://solitebrowser,org/d1/
BSc V - Python Programming(NEP) -2023-24 Page 9The SQLite3 module: The sqlite3 module is a built-in module in Python that provides a
simple and easy-to-use interface for working with SQLite databases. It allows you to create,
conne:
ct to, and manipulate SQLite databases using SQL commands,
To use the sqlite3 module in your Python code, you first need to import it by adding the
following line at the beginning of your code:
import solite3
SQLite Methods - connect, cursor, execute, close;
The sqlite3 module in Python provides several methods for working with SQLite databases:
Connect to Database:
1
connect: connect() method: This method is used to create a connection to an SQLite database.
It takes « single argument - the name of the database file - and returns a connection object that
can be used to execute SQL commands.
If the database file does not exist, it will be created automatically
Here's an example:
import sqlite3
# Create a connection to the database
conn
sqlite3.connect(‘example.db')
Cursor Object and its methods: A cursor object is used to interact with a database, A cursor
is created by a database connection object, and it is used to execute SQL. statements and retrieve
data from the database.
2. cursor method: This method is used to create a cursor object that can be used to execute
SQL commands. It is called on a connection object and returns a cursor object. Here's an
example:
# Create a cursor object
= conn.cursor()
3. execute() method: This method is used to execute an SQL command. It takes a single
argument - the SQL command - and returns a cursor object that can be used to fetch the results.
Here's an example:
yerson (id integer, name text)")
4. fetchone() method: This method is u
tuple of values or None if there are no more rows to fetch, Here's an exa
ed to fetch the next row of a query result set. It returns a
le:
# Fetch the next row
row = cfetchone),
BSc V - Python Programming(NEP) -2023-24 Page 105. fetchall) method: This method is used to fetch all the rows of a query result set. It returns a
list of tuples of values, Here's an example:
# Fetch all the rows
rows = e.fetchall)
6. commit) method: This method is used to save the changes made to the databass
on a connection object and commits the current transaction, Here's an example:
It is called
# Commit the changes
conn.commit()
7. close() method: This method is used to close the cursor and the connection. It is called on both
the cursor object and the connection object. Here's an example:
# Close the cursor and the connection
c.close()
jose
Operations on Tables Create, Insert, Select, Update. Delete and Drop Records.
import sqlite3
# create a connection to a SC/Lite database
conn = sqlite3.connect(‘test.dbl)
cursor = conn.cursor()
# create a table
cursor.execute("CREATE TABLE employee (id INTEGER, name TEXT)")
# insert records into the table
cursor.execute(“INSERT INTO employee(id,name) VALUES (101, 'Ravi
INSERT INTO employee(id,name) VALUES (102, ‘Ravishankar’")
cursor.execute("INSERT INTO employee(id,name) VALUES (103, ‘Ramesh’
cursor.execut
# select records from the table
cursorexecute("SELECT " FROM employee")
cursor.fetchall()
for row in rows:
print(row)
# update a record in the table.
cursor.execute("update employee set name = ‘Rakesh’ where id = 101")
BSc V - Python Programming(NEP) -2023-24 Page 11# delete a record from the table
cursor.execute("“delete from employee where id = 103")
# drop the table
cursor.execute("DROP TABLE employee")
#commit the transac!
conn.commit()
# close the cursor and connection
cursor.closeQ)
conn.close()
BSc V - Python Programming(NEP) -2023-24 Page 12