Week 6
Week 6
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:
PROGRAM:
1
WEEK 6 Dr MEERA ALPHY
OUTPUT:
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
self.width = width
self.height = height
self.color = color # New color attribute
2
WEEK 6 Dr MEERA ALPHY
OUTPUT:
AIM:
3
WEEK 6 Dr MEERA ALPHY
PROGRAM:
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
self.radius = radius
5
WEEK 6 Dr MEERA ALPHY
self.color = color
def draw_circle(canvas, circle):
x0 = circle.x - circle.radius
y0 = circle.y - circle.radius
x1 = circle.x + circle.radius
y1 = circle.y + circle.radius
canvas.create_oval(x0, y0, x1, y1, fill=circle.color, outline='black')
root = tk.Tk()
root.title("Draw Circles")
canvas = tk.Canvas(root, width=400, height=400, bg='white')
canvas.pack()
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)
root.mainloop()
OUTPUT:
6
WEEK 6 Dr MEERA ALPHY
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()
obj.show()
# Print MRO
7
WEEK 6 Dr MEERA ALPHY
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
OUTPUT: