Professional Documents
Culture Documents
____________________________________________________________________________
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.
3. GUI Components: Familiarity with basic graphical user interface components such as
buttons, labels, text fields, and windows.
2
Programming with Python (22616)
____________________________________________________________________________
____________________________________________________________________________
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")
3
Programming with Python (22616)
____________________________________________________________________________
row_val = 1
col_val = 0
# Clear button
tk.Button(root, text='C', padx=20, pady=20, font=('Arial', 16),
command=clear_entry).grid(row=row_val, column=col_val)
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.
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")
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
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
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
This line creates a clear button that calls the `clear_entry` function when clicked.
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)
____________________________________________________________________________
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
____________________________________________________________________________