You are on page 1of 8

Programming with Python (22616)

____________________________________________________________________________

Augmented practical :- Learn How to create Graphical Interface in Python to


create small Project
Relevant Program Outcomes (POs):
PO1. Basic and Discipline specific knowledge: Apply knowledge of basic mathematics, science
and engineering fundamentals and engineering specialization to solve the engineering problems.
PO2. Problem analysis: Identify and analyze well-defined engineering problems using codified
standard methods.
PO3. Design/ development of solutions: Design solutions for well-defined technical problems
and assist with the design of systems components or processes to meet specified needs.
PO4. Engineering Tools, Experimentation and Testing: Apply modern engineering tools and
appropriate technique to conduct standard tests and measurements.
PO5. Engineering practices for society, sustainability and environment: Apply appropriate
technology in context of society, sustainability, environment and ethical practices.
PO6. Project Management: Use engineering management principles individually, as a team
member or a leader to manage projects and effectively communicate about well-defined
engineering activities.
PO7. Life-long learning: Ability to analyze individual needs and engage in updating in the
context of technological changes.
Program Specific Outcomes (PSOs):
PSO1.Computer software and Hardware usage: use state – of- art- technologies for operation and
application of computer software and hardware.
PSO2.computer engineering Maintenance: Maintain computer engineering related software and
hardware systems.
I. Practical Significance:
Creating a graphical interface in Python is of significant practical importance as it introduces
students to the world of user interface design and development. Graphical interfaces are
fundamental components of many software applications, providing users with an interactive and
visually appealing experience. This practical will empower students to apply their Python
programming skills to build user-friendly applications with graphical elements, enhancing their
ability to develop software that meets user expectations.

II. Competency and Practical Skills:

1
Programming with Python (22616)
____________________________________________________________________________

Upon completing this practical, students are expected to develop the following competencies
and practical skills:

1. Graphical Interface Design: Design and create a graphical user interface (GUI) for a small
project using Python.

2. User Interaction: Implement interactive elements within the GUI, allowing users to input
data, trigger actions, and receive feedback.

3. Event Handling: Gain proficiency in handling events such as button clicks, mouse
movements, and keyboard inputs within the graphical interface.

4. Integration of Functionality: Integrate functionality into the GUI, demonstrating the ability to
connect the graphical elements with underlying Python code.

5. Error Handling: Implement error handling mechanisms to enhance the robustness of the
graphical interface.

III. Relevant Affective Domain related Outcome(s):


1. Engagement and Interest: Cultivate students' interest in software development by exposing
them to the visually engaging aspect of creating graphical interfaces.

2. Creativity: Encourage students to express creativity in designing user interfaces, fostering an


appreciation for the aesthetic aspects of software development.

3. Problem-Solving: Develop problem-solving skills by addressing challenges related to user


interaction and graphical design within the context of a small project.

IV. Minimum Theoretical Background:


Students undertaking this practical should have a foundational understanding of the following
concepts:

1. Python Programming: Proficiency in basic Python programming concepts, including


variables, data types, functions, and control flow.

2. Event-Driven Programming: Understanding of event-driven programming concepts, where


actions or events trigger specific functions or responses.

3. GUI Components: Familiarity with basic graphical user interface components such as
buttons, labels, text fields, and windows.

4. Object-Oriented Programming (OOP): Basic knowledge of OOP principles, as GUI


development in Python often involves the use of object-oriented concepts.

2
Programming with Python (22616)
____________________________________________________________________________

By incorporating these theoretical elements, students can seamlessly transition from


conceptualizing their small projects to implementing functional graphical interfaces using
Python.

____________________________________________________________________________
Example :

Creating a graphical interface in Python involves using a library such as Tkinter, PyQt, or Kivy.
I'll provide you with a simple example using Tkinter, which is a standard GUI library for Python.

Let's create a small project to build a basic calculator with a graphical interface using Tkinter:

import tkinter as tk

def on_click(button_value):
current_text = entry.get()
entry.delete(0, tk.END)
entry.insert(tk.END, current_text + str(button_value))

def clear_entry():
entry.delete(0, tk.END)

def calculate_result():
try:
result = eval(entry.get())
entry.delete(0, tk.END)
entry.insert(tk.END, str(result))
except Exception as e:
entry.delete(0, tk.END)
entry.insert(tk.END, "Error")

# Create the main window


root = tk.Tk()
root.title("Simple Calculator")

# Entry widget to display the input and results


entry = tk.Entry(root, width=20, font=('Arial', 16), bd=5, insertwidth=4)
entry.grid(row=0, column=0, columnspan=4)

# Buttons for numbers and operations


buttons = [
'7', '8', '9', '/',
'4', '5', '6', '*',

3
Programming with Python (22616)
____________________________________________________________________________

'1', '2', '3', '-',


'0', '.', '=', '+'
]

row_val = 1
col_val = 0

for button in buttons:


tk.Button(root, text=button, padx=20, pady=20, font=('Arial', 16),
command=lambda button_value=button: on_click(button_value) if button_value != '='
else calculate_result()).grid(row=row_val, column=col_val)
col_val += 1
if col_val > 3:
col_val = 0
row_val += 1

# Clear button
tk.Button(root, text='C', padx=20, pady=20, font=('Arial', 16),
command=clear_entry).grid(row=row_val, column=col_val)

# Run the Tkinter event loop


root.mainloop()

Copy and paste this code into a Python file (e.g., `calculator.py`) and run it. This code creates a
simple calculator with a graphical interface using Tkinter. It includes buttons for digits, arithmetic
operations, a clear button, and an equals button for calculating results.

Feel free to explore and modify the code to add more features or customize the GUI according
to your preferences. This is a great starting point to learn how to create graphical interfaces in
Python using Tkinter.

Output of Above Example:-

4
Programming with Python (22616)
____________________________________________________________________________

____________________________________________________________________________
Explanation

Let's break down the code step by step to understand how it creates a graphical interface for a
simple calculator using Tkinter in Python:

Importing Tkinter

import tkinter as tk

This line imports the Tkinter module and aliases it as `tk`. Tkinter is the standard GUI (Graphical
User Interface) toolkit that comes with Python.

Function Definitions

def on_click(button_value):
current_text = entry.get()
entry.delete(0, tk.END)
entry.insert(tk.END, current_text + str(button_value))

def clear_entry():
entry.delete(0, tk.END)

5
Programming with Python (22616)
____________________________________________________________________________

def calculate_result():
try:
result = eval(entry.get())
entry.delete(0, tk.END)
entry.insert(tk.END, str(result))
except Exception as e:
entry.delete(0, tk.END)
entry.insert(tk.END, "Error")

These are three functions defined for the calculator:


- `on_click`: Called when a digit or operation button is clicked. It updates the entry widget with
the clicked button's value.
- `clear_entry`: Called when the "C" button is clicked. It clears the entry widget.
- `calculate_result`: Called when the "=" button is clicked. It evaluates the expression in the
entry widget and displays the result. If there's an error in the expression, it displays "Error".

Create the Main Window

root = tk.Tk()
root.title("Simple Calculator")

This code creates the main window for the application and sets its title to "Simple Calculator".

Entry Widget

entry = tk.Entry(root, width=20, font=('Arial', 16), bd=5, insertwidth=4)


entry.grid(row=0, column=0, columnspan=4)

This code creates an entry widget (a text box) at the top of the window. The entry widget is
where the user can input numbers and see the results. The `grid` method is used to place the
entry widget at the specified row and column in the window.

Buttons

buttons = [
'7', '8', '9', '/',
'4', '5', '6', '*',
'1', '2', '3', '-',
'0', '.', '=', '+'
]

row_val = 1

6
Programming with Python (22616)
____________________________________________________________________________

col_val = 0

for button in buttons:


tk.Button(root, text=button, padx=20, pady=20, font=('Arial', 16),
command=lambda button_value=button: on_click(button_value) if button_value != '='
else calculate_result()).grid(row=row_val, column=col_val)
col_val += 1
if col_val > 3:
col_val = 0
row_val += 1

This part of the code creates buttons for digits (0-9), arithmetic operations (+, -, *, /), decimal
point (.), equals (=), and a clear button (C). The buttons are arranged in a grid using the `grid`
method. The lambda function is used to pass the corresponding button value to the `on_click`
function when the button is clicked.

Clear Button

tk.Button(root, text='C', padx=20, pady=20, font=('Arial', 16),


command=clear_entry).grid(row=row_val, column=col_val)

This line creates a clear button that calls the `clear_entry` function when clicked.

Run the Tkinter Event Loop

root.mainloop()

This line starts the Tkinter event loop, which is necessary for the GUI to respond to user
interactions. The program will continue running as long as the main window is open.

Summary
In summary, this Python script uses Tkinter to create a simple calculator with a graphical
interface. Users can input numbers and perform basic arithmetic operations by clicking on the
provided buttons. The script demonstrates the use of Tkinter widgets, event handling, and
layout management to create an interactive graphical user interface.

7
Programming with Python (22616)
____________________________________________________________________________

Computer Technology Department


Vision:To become a PREMIER Computer Technology Department for Students seeking
EXCELLENCE in Technical Education.

Mission:
M1- Effective implementation of Teaching Learning process in Classroom and
Laboratories
M2- To apply and impart Technical Knowledge and Trends in Computer Technology for
students.
M3- To create applications through comprehensive Educational Programs in
collaboration with Industry.
____________________________________________________________________________
Co. No. Course Outcome Statement
CI602.1 Use 3 Python IDE to display message on screen
CI602.2 Implement 3 Python program to demonstrate use of operators
CI602.3 State 1 operations on data structures in Python
CI602.4 Implement 3 functions for given problem
CI602.5 Define 1 & Demonstrate 3 classes for given problem
CI602.6 Use 3 Exception Handling
____________________________________________________________________________

You might also like