Assignment
Assignment
Assignment
Submitted To:
Md. Rakibul Hasan
Assistant Professor
Department of Management Information Systems
Faculty of Business Studies
University of Dhaka
Submitted By:
Akhi Kulsum Shifa
029-14-102
Section: B
window=Tk()
label=Label(window, text="Welcome to Python")
button=Button(window,text="Click Me")
label.pack ()
button.pack()
window.mainloop()
window=Tk()
btOK=Button(window,text="OK",fg="red",command=processOK)
btCancel=Button(window,text="Cancel",bg="yellow",command=processCancel)
btOK.pack()
btCancel.pack()
window.mainloop()
class ProcessButtonEvent:
def __init__(self):
window=Tk()
btOK =Button(window,text="OK",fg="red",
command=self.processOK)
btCancel=Button(window,text="Cancel",bg="yellow"
,command=self.processCancel)
btOK.pack()
btCancel.pack()
window.mainloop()
def processOK(self):
print("OK button is clicked")
def processCancel(self):
print("Cancel button is clicked")
ProcessButtonEvent()
In [ ]: from tkinter import* #Listing 9.4
class WidgetsDemo:
def __init__(self):
window=Tk()
window.title("Widgets Demo")
frame1=Frame(window)
frame1.pack()
self.v1=IntVar()
cbtBold=Checkbutton(frame1,text="Bold",
variable=self.v1,
command=self.processCheckbutton)
self.v2=IntVar()
rbRed=Radiobutton(frame1,text="Red", bg="red",
variable=self.v2, value=1,
command=self.processRadiobutton)
rbYellow=Radiobutton(frame1,text="Yellow", bg="yellow",
variable=self.v2, value=2,
command=self.processRadiobutton)
cbtBold.grid(row=1,column=1)
rbRed.grid(row=1,column=2)
rbYellow.grid(row=1,column=3)
frame2=Frame(window)
frame2.pack()
label=Label(frame2,text="Enter your name:")
self.name=StringVar()
entryName=Entry(frame2,textvariable=self.name)
btGetName=Button(frame2,text="Get Name",command=self.processButton)
message=Message(frame2,text="It is a widgets demo")
label.grid(row=1,column=1)
entryName.grid(row=1,column=2)
btGetName.grid(row=1,column=3)
message.grid(row=1,column=4)
text=Text(window)
text.pack()
text.insert(END,"Tip\nThe Best Way to Learn Tkinter is to read")
text.insert(END," these carefully designed examples and use them")
text.insert(END,"to create your applications.")
window.mainloop()
def processCheckbutton(self):
print("check Button is" + (" checked" if self.v1.get()==1 else"unchecked"
def processRadiobutton(self):
print(("Red" if self.v2.get()==1 else "Yellow" )+" is selected")
def processButton(self):
print("Your name is " + self.name.get())
WidgetsDemo()
In [ ]: from tkinter import * #Listing 9.5
class ChangeLabelDemo:
def __init__(self):
window = Tk()
window.title("Change Label Demo")
frame1 = Frame(window)
frame1.pack()
self.lbl = Label(frame1, text = "Programming is fun")
self.lbl.pack()
frame2 = Frame(window)
frame2.pack()
label = Label(frame2, text = "Enter text: ")
self.msg = StringVar()
entry = Entry(frame2, textvariable = self.msg)
btChangeText = Button(frame2, text = "Change Text",
command = self.processButton)
self.v1 = StringVar()
rbRed = Radiobutton(frame2, text = "Red", bg = "red",
variable = self.v1, value = 'R',
command = self.processRadiobutton)
rbYellow = Radiobutton(frame2, text = "Yellow",bg = "yellow",
variable = self.v1, value = 'Y',
command = self.processRadiobutton)
label.grid(row = 1, column = 1)
entry.grid(row = 1, column = 2)
btChangeText.grid(row = 1, column = 3)
rbRed.grid(row = 1, column = 4)
rbYellow.grid(row = 1, column = 5)
window.mainloop()
def processRadiobutton(self):
if self.v1.get()== 'R':
self.lbl["fg"] = "red"
elif self.v1.get()=="Y":
self.lbl["fg"]="yellow"
def processButton(self):
self.lbl["text"]=self.msg.get()
ChangeLabelDemo()
In [ ]: from tkinter import * #Listing 9.6
class CanvasDemo:
def __init__(self):
window = Tk()
window.title("Canvas Demo")
self.canvas=Canvas(window,width=200,height=100,bg="white")
self.canvas.pack()
frame = Frame(window)
frame.pack()
btRectangle = Button(frame, text = "Rectangle",
command = self.displayRect)
btOval = Button(frame, text = "Oval",
command = self.displayOval)
btArc = Button(frame, text = "Arc",
command = self.displayArc)
btPolygon = Button(frame, text = "Polygon",
command = self.displayPolygon)
btLine = Button(frame, text = "Line",
command = self.displayLine)
btString = Button(frame, text = "String",
command = self.displayString)
btClear = Button(frame, text = "Clear",
command = self.clearCanvas)
btRectangle.grid(row = 1, column = 1)
btOval.grid(row = 1, column = 2)
btArc.grid(row = 1, column = 3)
btPolygon.grid(row = 1, column = 4)
btLine.grid(row = 1, column = 5)
btString.grid(row = 1, column = 6)
btClear.grid(row = 1, column = 7)
window.mainloop()
def displayRect(self):
self.canvas.create_rectangle(10,10,190,90,tags="rect")
def displayOval(self):
self.canvas.create_oval(10, 10, 190, 90,fill = "red",tags="oval")
def displayArc(self):
self.canvas.create_arc(10, 10, 190, 90,
start=0,extent=90,
width=8, fill = "red",tags="arc")
def displayPolygon(self):
self.canvas.create_polygon(10, 10, 190, 90,30,50,tags="polygon")
def displayLine(self):
self.canvas.create_line(10, 10, 190, 90,fill="red",tags="line")
self.canvas.create_line(10, 90, 190, 10, width=9, arrow="last",
activefill="yellow",tags="line")
def displayString(self):
self.canvas.create_text(60,40,text="Hi, I am a string",
font="Times 10 bold underline",tags="string")
def clearCanvas(self):
self.canvas.delete("rect","oval","arc","polygon","line","string")
()
In [ ]: from tkinter import * #Listing 9.7
class GridManagerDemo:
window = Tk()
window.title("Grid Manager Demo")
message = Message(window,
text = "This Message widget occupies three rows and two columns")
message.grid(row = 1, column = 1, rowspan=3 , columnspan = 2)
Label(window, text = "First Name:").grid(row = 1, column = 3)
Entry(window).grid(row = 1, column = 4, padx=5 , pady = 5)
Label(window, text = "Last Name:").grid(row = 2, column = 3)
Entry(window).grid(row = 2, column = 4)
Button(window, text = "Get Name").grid(row = 3, padx = 5,
pady = 5, column = 4,sticky=E )
window.mainloop()
GridManagerDemo()
class PackManagerDemo:
def __init__(self):
window = Tk()
window.title("Pack Manager Demo 1")
window.mainloop()
PackManagerDemo()
In [ ]: from tkinter import * #Listing 9.9
class PackManagerDemoWithSide:
window = Tk()
window.title("Pack Manager Demo 2")
window.mainloop()
PackManagerDemoWithSide()
In [ ]: from tkinter import * #Listing 9.10
class PlaceManagerDemo:
def __init__(self):
window = Tk()
window.title("Place Manager Demo")
window.mainloop()
PlaceManagerDemo()
In [2]: from tkinter import * #Listing 9.11
class LoanCalculator:
def __init__(self):
window = Tk()
window.title("Loan Calculator")
self.numberOfYearsVar = StringVar()
Entry(window, textvariable = self.numberOfYearsVar,
justify = RIGHT).grid(row = 2, column = 2)
self.loanAmountVar=StringVar()
Entry(window, textvariable = self.loanAmountVar,
justify = RIGHT).grid(row = 3, column = 2)
self.monthlyPaymentVar=StringVar()
lblMonthlyPayment = Label(window,
textvariable=self.monthlyPaymentVar
).grid(row = 4,column = 2, sticky = E)
self.totalPaymentVar = StringVar()
lblTotalPayment = Label(window,
textvariable =self.totalPaymentVar
).grid(row = 5,column=2,sticky=E)
btComputePayment=Button(window,text="Compute Payemet",
command=self.computePayment).grid(row=6,column=2,sticky=E)
window.mainloop()
def computePayment(self):
monthlyPayment=self.getMonthlyPayment(
float(self.loanAmountVar.get()),
float(self.annualInterestRateVar.get())/1200,
int(self.numberOfYearsVar.get()))
self.monthlyPaymentVar.set(format(monthlyPayment,"10.2f"))
totalPayment=float(self.monthlyPaymentVar.get())*12 \
*int(self.numberOfYearsVar.get())
self.totalPaymentVar.set(format(totalPayment,"10.2f"))
def getMonthlyPayment(self,loanAmount,
monthlyInterestRate,numberOfYears):
monthlyPayment=loanAmount*monthlyInterestRate/(1-1/
(1+
monthlyInterestRate)**(numberOfYears**12))
return monthlyPayment;
LoanCalculator()
Out[2]: <__main__.LoanCalculator at 0x1f58f074708>
frame1 = Frame(window)
frame1.pack()
Label(frame1, image-caImage ).pack(side = LEFT)
canvas = Canvas(frame1)
canvas.create_image(90, 50, image = chinaImage)
canvas["width"] = 200
canvas["height"] = 100
canvas.pack(side = LEFT)
frame2 = Frame(window)
frame2.pack()
Button(frame2, image = leftImage).pack(side = LEFT)
Button(frame2, image = rightImage).pack(side = LEFT)
Checkbutton(frame2, image = usImage).pack(side = LEFT)
Checkbutton(frame2, image = ukImage).pack(side = LEFT)
Radiobutton(frame2, image = crossImage).pack(side = LEFT)
Radiobutton(frame2, image = circleImage).pack(side = LEFT)
window.mainloop()
()
In [ ]: from tkinter import * #Listing 9.13
class MenuDemo:
def __init__(self):
window = Tk()
window.title("Menu Demo")
menubar = Menu(window)
window.config(menu = menubar)
frame0 = Frame(window)
frame0.grid(row = 1, column = 1, sticky = W)
frame1 = Frame(window)
frame1.grid(row = 2, column = 1, pady = 10)
Label(frame1, text = "Number 1:").pack(side = LEFT)
self.v1 = StringVar()
Entry(frame1, width = 5, textvariable = self.v1,
justify = RIGHT).pack(side = LEFT)
Label(frame1, text = "Number 2:").pack(side = LEFT)
self.v2 = StringVar()
Entry(frame1, width = 5, textvariable = self.v2,
justify = RIGHT).pack(side = LEFT)
Label(frame1, text = "Result:").pack(side = LEFT)
self.v3 = StringVar()
Entry(frame1, width = 5, textvariable = self.v3,
justify = RIGHT).pack(side = LEFT)
frame2 = Frame(window)
frame2.grid(row = 3, column = 1, pady = 10, sticky = E)
Button(frame2, text = "Add", command = self.add).pack(side=LEFT)
mainloop()
def add(self):
self.v3.set(eval(self.v1.get()) + eval(self.v2.get()))
def subtract(self):
self.v3.set(eval(self.v1.get()) - eval(self.v2.get()))
def multiply(self):
self.v3.set(eval(self.v1.get()) * eval(self.v2.get()))
def divide(self):
self.v3.set(eval(self.v1.get()) / eval(self.v2.get()))
MenuDemo()
In [ ]: from tkinter import * #Listing 9.14
class PopupMenuDemo:
def __init__(self):
window = Tk()
window.title("Popup Menu Demo")
self.canvas.bind("<Button-3>", self.popup)
window.mainloop()
def displayRect(self):
self.canvas.create_rectangle(10, 10, 190, 90, tags = "rect")
def displayOval(self):
self.canvas.create_oval(10, 10, 190, 90, tags = "oval")
def displayLine(self):
self.canvas.create_line(10, 10, 190, 90, tags = "line")
self.canvas.create_line(10, 90, 190, 10, tags = "line")
def clearCanvas(self):
self.canvas.delete("rect", "oval", "line")
PopupMenuDemo()
In [ ]: from tkinter import * #Listing 9.15
class MouseKeyEventDemo:
def __init__(self):
window = Tk()
window.title("Event Demo")
canvas = Canvas(window, bg = "white", width = 200, height = 100)
canvas.pack()
canvas.bind("<Button-1>", self.processMouseEvent)
canvas.bind("<Key>", self.processKeyEvent)
canvas.focus_set()
window.mainloop()
MouseKeyEventDemo()
In [ ]: from tkinter import * #Listing 9.16
class EnlargeShrinkCircle:
def __init__(self):
self.radius = 50
window = Tk()
window.title("Control Circle Demo")
self.canvas = Canvas(window, bg = "white",
width = 200, height = 200)
self.canvas.pack()
self.canvas.create_oval(
100 - self.radius, 100 - self.radius,
100 + self.radius, 100 + self.radius, tags = "oval")
self.canvas.bind("<Button-3>", self.decreaseCircle)
self.canvas.bind("<Button-1>", self.increaseCircle)
window.mainloop()
EnlargeShrinkCircle()
In [ ]: from tkinter import * #Listing 9.17
class AnimationDemo:
def __init__(self):
window = Tk()
window.title("Animation Demo")
width = 250
canvas=Canvas(window, bg="white",width=250,height=50)
canvas.pack()
x = 0
canvas.create_text(x,30,text="Message moving?", tags = "text")
dx = 3
while True:
canvas.move("text",dx,0)
canvas.after(100)
canvas.update()
if x < width:
x += dx
else:
x = 0
canvas.delete("text")
canvas.create_text(x, 30, text = "Message moving?", tags = "text")
window.mainloop()
AnimationDemo()
In [ ]: from tkinter import * #Listing 9.18
class ControlAnimation:
def __init__(self):
window = Tk()
window.title("Control Animation Demo")
self.width = 250
self.canvas=Canvas(window, bg = "white", width = self.width, height =
self.canvas.pack()
frame = Frame(window)
frame.pack()
btStop = Button(frame, text = "Stop", command = self.stop)
btStop.pack(side = LEFT)
btResume = Button(frame, text = "Resume", command = self.resume)
btResume.pack(side = LEFT)
btFaster = Button(frame, text = "Faster", command = self.faster)
btFaster.pack(side = LEFT)
btSlower = Button(frame, text = "Slower", command = self.slower)
btSlower.pack(side = LEFT)
self.x= 0
self.sleepTime=100
self.canvas.create_text(self.x, 30, text = "Message moving?", tags = "text"
self.dx=3
self.isStopped=False
self.animate()
window.mainloop()
def stop(self):
self.isStopped=True
def resume(self):
self.isStopped=False
self.animated()
def faster(self):
if self.sleepTime>5:
self.sleepTime-=20
def slower(self):
self.sleepTime+=20
def animated(self):
while not self.isStopped:
self.canvas.move("text",self.dx,0)
self.canvas.after(self.sleepTime)
self.canvas.update()
if self.x<self.width:
self.x += self.dx
else:
self.x=0
self.canvas.delete("text")
self.canvas.create_text(self.x, 30 ,text = "Message moving?", tags
ControlAnimation()
In [ ]: from tkinter import * #Listing 9.19
class ScrollText:
def __init__(self):
window = Tk()
window.title("Scroll Text Demo")
frame1 = Frame(window)
frame1.pack()
scrollbar = Scrollbar(frame1)
scrollbar.pack(side = RIGHT, fill = Y)