0% found this document useful (0 votes)
40 views9 pages

Drawing Circles with Radius in Python

The document outlines a series of programming exercises using Python's Tkinter library to create graphical representations of shapes such as rectangles, points, and circles. It also includes a demonstration of Method Resolution Order (MRO) in multiple levels of inheritance and a validation program for phone numbers and email addresses. Each section provides aims, program code, and expected outputs for clarity.

Uploaded by

lokeshisukala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views9 pages

Drawing Circles with Radius in Python

The document outlines a series of programming exercises using Python's Tkinter library to create graphical representations of shapes such as rectangles, points, and circles. It also includes a demonstration of Method Resolution Order (MRO) in multiple levels of inheritance and a validation program for phone numbers and email addresses. Each section provides aims, program code, and expected outputs for clarity.

Uploaded by

lokeshisukala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

WEEK 6 Dr MEERA ALPHY

1. 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.
AIM:

• To define a function draw_rectangle that accepts a Canvas and a Rectangle


as input parameters.
• To access the attributes of the Rectangle (such as width, height, and position).
• To draw the rectangle accurately on the given canvas using the provided
dimensions and coordinates.

PROGRAM:

from tkinter import *


class Rectangle:
def __init__(self, x, y, width, height, color):
self.x = x
self.y = y
[Link] = width
[Link] = height
[Link] = color # New color attribute
def draw_rectangle(canvas, rect):
canvas.create_rectangle(
rect.x, rect.y,
rect.x + [Link], rect.y + [Link],
fill=[Link]
)
root = Tk()
canvas = Canvas(root, width=300, height=300)
[Link]()
r1 = Rectangle(30, 30, 100, 60, "red")
draw_rectangle(canvas, r1)
[Link]()

1
WEEK 6 Dr MEERA ALPHY

OUTPUT:

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.

AIM:
• To add a color attribute to the Rectangle class.
• To modify the draw_rectangle function to utilize the rectangle's color
attribute.
• To ensure the rectangle is drawn on the canvas with the specified fill color.

PROGRAM:
from tkinter import *
class Rectangle:
def __init__(self, x, y, width, height, color):
self.x = x
self.y = y
[Link] = width
[Link] = height
[Link] = color # New color attribute

2
WEEK 6 Dr MEERA ALPHY

def draw_rectangle(canvas, rect):


canvas.create_rectangle(
rect.x, rect.y,
rect.x + [Link], rect.y + [Link],
fill=[Link]
)
# Main program
root = Tk()
canvas = Canvas(root, width=300, height=300)
[Link]()
# Create and draw rectangles with different colors
r1 = Rectangle(30, 30, 100, 60, "red")
r2 = Rectangle(80, 120, 150, 80, "green")
r3 = Rectangle(60, 220, 120, 50, "blue")
draw_rectangle(canvas, r1)
draw_rectangle(canvas, r2)
draw_rectangle(canvas, r3)
[Link]()

OUTPUT:

AIM:

3
WEEK 6 Dr MEERA ALPHY

• To create a function draw_point that takes a Canvas and a Point as input.


• To access the coordinates of the Point object.
• To draw a visual representation of the point on the given canvas.

PROGRAM:

from tkinter import *


class Point:
def __init__(self, x, y, color="black"):
self.x = x
self.y = y
[Link] = color
def draw_point(canvas, point):
r = int(input("enter the radius"))# radius of the point (to make it visible)
canvas.create_oval(
point.x - r, point.y - r,
point.x + r, point.y + r,
fill=[Link], outline=[Link]
)
root = Tk()
canvas = Canvas(root, width=300, height=300,bg="pink")
[Link]()
# Create and draw some points
p1 = Point(50, 50, "red")
p2 = Point(100, 100, "blue")
p3 = Point(200, 200, "green")
draw_point(canvas, p1)
draw_point(canvas, p2)
draw_point(canvas, p3)
[Link]()

4
WEEK 6 Dr MEERA ALPHY

OUTPUT:

d. Define a new class called Circle with appropriate attributes and instantiate a few
Circle objects. Write a function called draw circle that draws circles on the canvas.

AIM:
• To define a new class Circle with relevant attributes such as center, radius,
and color.
• To create and instantiate multiple Circle objects.
• To write a draw_circle function that draws these circles on the canvas using
their attributes.

PROGRAM:
import tkinter as tk
class Circle:
def __init__(self, x, y, radius, color='blue'):
self.x = x
self.y = y
[Link] = radius
5
WEEK 6 Dr MEERA ALPHY

[Link] = color
def draw_circle(canvas, circle):
x0 = circle.x - [Link]
y0 = circle.y - [Link]
x1 = circle.x + [Link]
y1 = circle.y + [Link]
canvas.create_oval(x0, y0, x1, y1, fill=[Link], outline='black')
root = [Link]()
[Link]("Draw Circles")
canvas = [Link](root, width=400, height=400, bg='white')
[Link]()
circle1 = Circle(100, 100, 40, 'red')
circle2 = Circle(200, 150, 60, 'green')
circle3 = Circle(300, 250, 50, 'blue')
for circle in [circle1, circle2, circle3]:
draw_circle(canvas, circle)
[Link]()

OUTPUT:

6
WEEK 6 Dr MEERA ALPHY

2. Write a Python program to demonstrate the usage of Method Resolution


Order(MRO) in multiple levels of Inheritances.
AIM:
1. To understand and implement Method Resolution Order (MRO) in Python.
2. To demonstrate MRO in a program involving multiple levels of inheritance.
3. To observe how Python determines the method to execute when there are
multiple inherited classes.

PROGRAM:

class A:
def show(self):
print("Class A")
class B(A):
def show(self):
print("Class B")
super().show()
class C(A):
def show(self):
print("Class C")
super().show()
class D(B, C):
# D inherits from B and C
def show(self):
print("Class D")
super().show()
# Create object of class D
obj = D()
[Link]()
# Print MRO

7
WEEK 6 Dr MEERA ALPHY

print("Method Resolution Order:")


for cls in [Link]():
print(cls)

OUTPUT:

3. Write a python code to read a phone number and email-id from the user and
validate it for Correctness.

AIM:
• To read a phone number and email ID as input from the user.
• To validate the correctness of the phone number and email ID using regular
expressions.
• To ensure the inputs meet standard formatting rules for phone numbers and
email addresses.

PROGRAMS:
phone = input("Enter your phone number: ")
email = input("Enter your email ID: ")

8
WEEK 6 Dr MEERA ALPHY

if len(phone) == 10 and [Link]():


print("Phone number is valid.")
else:
print("Invalid phone number.")
if "@" in email and "." in email:
print("Email ID is valid.")
else:
print("Invalid email ID.")

OUTPUT:

You might also like