- List of Experiments
- Week 1
- Week 2
- Week 3
- Week 4
- Week 5
- Week 6
- Week 7
- Week 8
Python Drawing Shapes with Classes (Rectangle, Circle, Point)
Aim:
a) Write a function called draw rectangle that takes a Canvas and a Rectangle as arguments and draws a representation of the Rectangle on the Canvas.
b) Add an attribute named color to your Rectangle objects and modify draw_rectangle so that it uses the color attribute as the fill color.
c) Write a function called draw_point that takes a Canvas and a Point as arguments and draws a representation of the Point on the Canvas.
d) Define a new class called Circle with appropriate attributes and instantiate a few Circleobjects. Write a function called draw_circle that draws circles on the canvas.
Program:
from tkinter import *
#canvas_width = 190
#canvas_height =150
master = Tk()
w = Canvas(master,width=400,height=400)
w.pack()
w.create_rectangle(100,100,200,200)
mainloop()
def draw_rectangle(canvas,x,y, r):
id = canvas.create_rectangle(x-r,y-r,x+r,y+r)
return id
Output:
Program:
#Import the library
from tkinter import *
#Create an instance of tkinter frame
win= Tk()
#Define the geometry of window
win.geometry("600x400")
#Create a canvas object
c= Canvas(win,width=400, height=400)
c.pack()
#Draw an Circle in the canvas
c.create_circle(60,60,210,210)
Output:
Sample Viva Questions and Answers
1. What is the purpose of the draw_rectangle
function?
The purpose of the draw_rectangle
function is to draw a rectangle on a given Tkinter canvas at specified coordinates with a specified size.
2. How does the draw_rectangle
function determine the position and size of the rectangle?
The function takes parameters x
, y
, and r
, where (x, y)
represents the center of the rectangle, and r
represents half the width and height of the rectangle.
3. What is the output of the draw_rectangle
function when called with specific parameters?
The output is the ID of the rectangle drawn on the canvas, which can be used for further manipulation (like moving or deleting the rectangle).
4. How can you modify the draw_rectangle
function to use a color attribute from a Rectangle object?
You can add a color
attribute to the Rectangle class and modify the create_rectangle
method to include the fill
parameter, like this:
id = canvas.create_rectangle(x-r, y-r, x+r, y+r, fill=r.color)
5. What is the purpose of the draw_point
function?
The purpose of the draw_point
function is to draw a representation of a point on the canvas at specified coordinates.
6. How would you define a new class called Circle
?
You would define the Circle
class with attributes such as center
(a Point object) and radius
, and include methods for drawing the circle on the canvas.
7. How can you draw a circle on the canvas using the draw_circle
function?
You can use the create_oval
method of the canvas to draw a circle by specifying the bounding box coordinates based on the circle’s center and radius.
8. What is the significance of the mainloop()
function in a Tkinter application?
The mainloop()
function starts the Tkinter event loop, allowing the application to wait for user interactions and update the GUI.
9. What will happen if you try to call c.create_circle(60,60,210,210)
without defining the create_circle
method?
If create_circle
is not defined, it will raise an AttributeError
because the Canvas class does not have a method named create_circle
.
10. Can you explain how to instantiate Circle objects and use them with the draw_circle
function?
You would create instances of the Circle class with specific attributes (like center and radius) and then call the draw_circle
function, passing the canvas and the Circle object to draw it on the canvas.
📌 Python Programming Lab – Complete Guide
Welcome to the Python Programming Lab! This comprehensive lab series is designed to provide a solid foundation in Python programming through practical exercises and hands-on learning. From basic calculations to advanced concepts like file handling, modules, and GUI programming, this lab covers it all. Let’s dive into what you’ll be learning throughout this lab course.
🔍 Week 1: Introduction to Python Basics
In the first week, you will explore fundamental Python concepts aimed at building a strong foundation for your programming journey.
- Getting Started with Python: Visit the Python Official Website to explore documentation and use the
help()
function in the interpreter. - Python as a Calculator: Learn how to perform basic arithmetic operations like addition, subtraction, multiplication, and division.
- Calculating Compound Interest: Write a program to calculate compound interest using the formula with given principal, rate, and time.
- Distance Calculation: Compute the distance between two points using the distance formula.
- Reading User Details: Write a program to collect and print user details like name, address, email, and phone number.
🔍 Week 2: Python Loops and Conditional Statements
This week focuses on using loops and conditional statements effectively.
- Pattern Generation: Print a triangle pattern using nested loops.
- Character Identification: Write a program to identify whether the input is a digit, lowercase, uppercase, or special character.
- Fibonacci Sequence Generation: Use a
while
loop to generate the Fibonacci series. - Prime Numbers Identification: Find all prime numbers within a specified range using the
break
statement.
🔍 Week 3: Data Structures and Functions
Dive into data structures and creating functions to enhance your programming skills.
- Converting Lists and Tuples to Arrays: Understand how to convert different data structures.
- Finding Common Values: Compare two arrays to find common values.
- Calculating GCD: Create a function to calculate the greatest common divisor (GCD) of two numbers.
- Palindrome Checker: Write a function that checks if a given string is a palindrome.
🔍 Week 4: Advanced Functions and String Manipulation
This week emphasizes sorting, handling duplicate elements, and manipulating strings effectively.
- Checking Sorted Lists: Write a function to check if a list is sorted.
- Handling Duplicates: Create functions to detect and remove duplicates from lists.
- Dictionary Inversion: Write a program to swap keys and values in a dictionary.
- String Manipulation: Add commas between characters, remove words from strings, and convert sentences to title case without using built-in functions.
- Binary String Generation: Use recursion to generate all binary strings of a specified length.
🔍 Week 5: Working with Matrices and Modules
Learn to work with matrices and create custom modules for various applications.
- Matrix Operations: Define, print, add, and multiply square matrices using Python.
- Creating Modules: Build modules using geometrical shapes and their operations.
- Exception Handling: Implement exception handling for robust error management.
🔍 Week 6: Object-Oriented Programming and Validation
This week focuses on using classes, inheritance, and validation techniques.
- Drawing Shapes with Classes:
- Create classes for rectangles, points, and circles, and draw them on a canvas.
- Add colors and attributes to these shapes to enhance visualization.
- Method Resolution Order (MRO): Demonstrate MRO in multiple inheritance scenarios.
- Data Validation: Write programs to validate phone numbers and email addresses.
🔍 Week 7: File Handling and Text Processing
Master file handling techniques and analyzing text data.
- File Merging: Combine the contents of two files into a new file.
- Word Search: Create a function to find specific words in a file.
- Word Frequency Analysis: Identify the most frequent words in a text file.
- Text Statistics: Count words, vowels, blank spaces, lowercase, and uppercase letters in a file.
🔍 Week 8: Python Libraries and GUI Programming
Explore advanced Python libraries and build simple graphical user interfaces.
- NumPy, Plotly, and Scipy: Learn how to install and use these powerful libraries for data analysis and visualization.
- Digital Logic Gates: Implement logic gate operations such as AND, OR, NOT, and EX-OR.
- Adder Circuits: Create programs for Half Adders, Full Adders, and Parallel Adders.
- GUI Programming: Build a simple window wizard with text labels, input fields, and buttons using Python’s GUI tools.
🌟 Conclusion:
The Python Programming Lab R23 provides you with a strong foundation in Python programming through a wide range of exercises, from basic concepts to advanced topics. By the end of this lab, you will have gained valuable skills in data processing, file handling, OOP, modules, and GUI development. Start coding and enhance your Python skills! 🚀