Python 5th Unit Notes
Python 5th Unit Notes
We are now stepping into making applications with graphical elements, we will learn how to
make cool apps and focus more on its GUI(Graphical User Interface) using Tkinter.
What is Tkinter?
Tkinter is a Python Package for creating GUI applications. Python has a lot of GUI
frameworks, but Tkinter is the only framework that’s built into the Python standard library.
Tkinter has several strengths; it’s cross-platform, so the same code works on Windows,
macOS, and Linux.
Tkinter is lightweight and relatively painless to use compared to other frameworks. This
makes it a compelling choice for building GUI applications in Python, especially for
applications where a modern shine is unnecessary, and the top priority is to build something
functional and cross-platform quickly.
Use Cases of Tkinter
1. Creating windows and dialog boxes: Tkinter can be used to create windows and dialog
boxes that allow users to interact with your program. These can be used to display
information, gather input, or present options to the user.
To create a window or dialog box, you can use the Tk() function to create a root window, and
then use functions like Label, Button, and Entry to add widgets to the window.
2. Building a GUI for a desktop application: Tkinter can be used to create the interface for
a desktop application, including buttons, menus, and other interactive elements.
To build a GUI for a desktop application, you can use functions like Menu, Checkbutton, and
RadioButton to create menus and interactive elements and use layout managers like pack and
grid to arrange the widgets on the window.
3. Adding a GUI to a command-line program: Tkinter can be used to add a GUI to a
command-line program, making it easier for users to interact with the program and input
arguments.
To add a GUI to a command-line program, you can use functions like Entry and Button to
create input fields and buttons, and use event handlers like command and bind to handle user
input.
4. Creating custom widgets: Tkinter includes a variety of built-in widgets, such as buttons,
labels, and text boxes, but it also allows you to create your own custom widgets.
To create a custom widget, you can define a class that inherits from the Widget class and
overrides its methods to define the behavior and appearance of the widget.
5. Prototyping a GUI: Tkinter can be used to quickly prototype a GUI, allowing you to test
and iterate on different design ideas before committing to a final implementation.
To prototype a GUI with Tkinter, you can use the Tk() function to create a root window, and
then use functions like Label, Button, and Entry to add widgets to the window and test
different layouts and design ideas.
Tkinter Alternatives
There are several libraries that are similar to Tkinter and can be used for creating graphical
user interfaces (GUIs) in Python. Some examples include:
1. PyQt: PyQt is a GUI library that allows you to create GUI applications using the Qt
framework. It is a comprehensive library with a large number of widgets and features.
2. wxPython: wxPython is a library that allows you to create GUI applications using the
wxWidgets framework. It includes a wide range of widgets in it’s GUI toolkit and is
cross-platform, meaning it can run on multiple operating systems.
3. PyGTK: PyGTK is a GUI library that allows you to create GUI applications using the
GTK+ framework. It is a cross-platform library with a wide range of widgets and
features.
4. Kivy: Kivy is a library that allows you to create GUI applications using a modern,
responsive design. It is particularly well-suited for building mobile apps and games.
5. PyForms: PyForms is a library that allows you to create GUI applications using a
simple, declarative syntax. It is designed to be easy to use and has a small footprint.
6. Pygame: PyForms is a library that is popular because you can develop video games
using it. It is a free, open source, and cross-platform wrapper for the Simple
DirectMedia Library (SDL). You can check Pygame Tutorial if you are interested in
video game development.
In summary, there are several libraries available for creating GUI applications in Python,
each with its own set of features and capabilities. Tkinter is a popular choice, but you may
want to consider other options depending on your specific needs and requirements.
To understand Tkinter better, we will create a simple GUI.
Getting Started with Tkinter
1. Import tkinter package and all of its modules.
2. Create a root window. Give the root window a title(using title()) and dimension(using
geometry()). All other widgets will be inside the root window.
3. Use mainloop() to call the endless loop of the window. If you forget to call this nothing
will appear to the user. The window will wait for any user interaction till we close it.
# Import Module
from tkinter import *
Root Window
4. We’ll add a label using the Label Class and change its text configuration as desired. The
grid() function is a geometry manager which keeps the label in the desired location inside the
window. If no parameters are mentioned by default it will place it in the empty cell; that is 0,0
as that is the first location.
# Import Module
from tkinter import *
# Execute Tkinter
root.mainloop()
Output
# Execute Tkinter
root.mainloop()
Output:
Button added
# Execute Tkinter
root.mainloop()
Output:
Entry Widget at column 2 row 0
Widget Description
A PanedWindow is a container
widget that may contain any number
PanedWindow
of panes, arranged horizontally or
vertically.
Geometry Management
All Tkinter widgets have access to specific geometry management methods, which have the
purpose of organizing widgets throughout the parent widget area. Tkinter exposes the
following geometry manager classes: pack, grid, and place. Their description is mentioned
below –
In this article, we have learned about GUI programming in Python and how to make GUI
in Python. GUI is a very demanded skill so you must know how to develop GUI using
Python. Hope this article helped you in creating GUI using Python.
Python - Databases and SQL
The Python programming language has powerful features for database programming. Python
supports various databases like SQLite, MySQL, Oracle, Sybase, PostgreSQL, etc. Python
also supports Data Definition Language (DDL), Data Manipulation Language (DML) and
Data Query Statements. The Python standard for database interfaces is the Python DB-API.
Most Python database interfaces adhere to this standard.
Here is the list of available Python database interfaces: Python Database Interfaces and APIs.
You must download a separate DB API module for each database you need to access.
In this chapter we will see the use of SQLite database in python programming language. It is
done by using python’s inbuilt, sqlite3 module. You should first create a connection object
that represents the database and then create some cursor objects to execute SQL statements.
Connect To Database
Following Python code shows how to connect to an existing database. If the database does
not exist, then it will be created and finally a database object will be returned.
#!/usr/bin/python
import sqlite3
conn = sqlite3.connect('test.db')
Create a Table
Following Python program will be used to create a table in the previously created database.
#!/usr/bin/python
import sqlite3
conn = sqlite3.connect('test.db')
print "Opened database successfully";
conn.close()
When the above program is executed, it will create the COMPANY table in your test.db and
it will display the following messages −
Opened database successfully
Table created successfully
Insert Operation
Following Python program shows how to create records in the COMPANY table created in
the above example.
#!/usr/bin/python
import sqlite3
conn = sqlite3.connect('test.db')
print "Opened database successfully";
conn.commit()
print "Records created successfully";
conn.close()
When the above program is executed, it will create the given records in the COMPANY table
and it will display the following two lines −
Opened database successfully
Records created successfully
Select Operation
Following Python program shows how to fetch and display records from the COMPANY
table created in the above example.
#!/usr/bin/python
import sqlite3
conn = sqlite3.connect('test.db')
print "Opened database successfully";
ID = 2
NAME = Allen
ADDRESS = Texas
SALARY = 15000.0
ID = 3
NAME = Teddy
ADDRESS = Norway
SALARY = 20000.0
ID = 4
NAME = Mark
ADDRESS = Rich-Mond
SALARY = 65000.0
import sqlite3
conn = sqlite3.connect('test.db')
print "Opened database successfully";
ID = 2
NAME = Allen
ADDRESS = Texas
SALARY = 15000.0
ID = 3
NAME = Teddy
ADDRESS = Norway
SALARY = 20000.0
ID = 4
NAME = Mark
ADDRESS = Rich-Mond
SALARY = 65000.0
Delete Operation
Following Python code shows how to use DELETE statement to delete any record and then
fetch and display the remaining records from the COMPANY table.
#!/usr/bin/python
import sqlite3
conn = sqlite3.connect('test.db')
print "Opened database successfully";
ID = 3
NAME = Teddy
ADDRESS = Norway
SALARY = 20000.0
ID = 4
NAME = Mark
ADDRESS = Rich-Mond
SALARY = 65000.0