Week-6:
- 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.
Answer:
from tkinter import *
master=Tk()
w=Canvas(master,width=200,height=100)
w.pack()
w.create_line(0,0,200,100)
w.create_line(0,100,200,0,fill='red',dash=(4,4))
w.create_rectangle(50,20,150,75,fill='blue')
master.mainloop()
Output:
- 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.
Answer:
from tkinter import*
canvas_width=500
canvas_height=150
def paint(event):
python_green="#476042"
x1, y1 = (event.x - 1), (event.y - 1)
x2,y2=(event.x +1),(event.y+1)
w.create_oval(x1,y1,x2,y2,fill=python_green)
master = Tk()
master.title("Points")
w=Canvas(master,width=canvas_width,height=canvas_height)
w.pack(expand=YES,fill=BOTH)
w.bind("<B1-Motion>", paint)
message=Label(master, text="Press and Drag the mouse to draw")
message.pack(side=BOTTOM)
mainloop()
Output:
- 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.
Answer:
from tkinter import*
canvas_width=190
canvas_height =150
master =Tk()
w=Canvas(master,width=canvas_width,height=canvas_height)
w.pack()
w.create_oval(50,50,150,100)
master.mainloop()
Output:
- Write a Python program to demonstrate the usage of Method Resolution Order (MRO) in multiple levels of Inheritances.
Answer:
class A:
def rk(self):
print(" In class A")
class B(A):
def rk(self):
print("Inclass B")
class C(A):
def rk(self):
print("Inclass C")
# classes ordering
class D(B,C):
pass
r=D()
r.rk()
Output:
Inclass B
- Write a python code to read a phone number and email-id from the user and validate it for correctness.
Answer:
import re
# Make a regular expression
# for validating an Email
Reg_exp= r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,7}\b'
# Define a function for
# for validating an Email
def check(email):
# pass the regular expression
# and the string into the fullmatch() method
if(re.fullmatch(reg_exp, email)):
print("Valid Email")
else:
print("Invalid Email")
# Driver Code
if __name__ == '__main__':
# Enter the email
email = "sateeshnagavarapu@gmail.com"
# calling run function
check(email)
email = "mrit@ac.in"
check(email)
email = "v2smartclasses.com"
check(email)
Output:
Valid Email
Valid Email
Invalid Email