0% found this document useful (0 votes)
27 views56 pages

Python U-5 One Shot Notes P

Python unit 5 notes
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)
27 views56 pages

Python U-5 One Shot Notes P

Python unit 5 notes
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
You are on page 1/ 56

#How to Create Window with Label and Button, and Text Box

import tkinter as tk
w=tk.Tk()
w.geometry('400x300')
l1=tk.Label(w,text='Example of Label')
l1.pack()
l2=tk.Label(w,text='First Name')
l2.pack()
t1=tk.Entry(w,width=40)
t1.pack()
b1=tk.Button(w,text="Click Here")
b1.pack()
t2=tk.Text(w)
t2.pack()
w.mainloop()

#How to Create Window with Label and Text box


import tkinter as tk
w=tk.Tk()
w.geometry('400x300')
l1=tk.Label(w,text="First Name:")
l1.grid(row=0,column=0)
l2=tk.Label(w,text="Last Name:")
l2.grid(row=1,column=0)
l3=tk.Label(w,text="Middle Name:")
l3.grid(row=2,column=0)
t1=tk.Entry(w,width=40)
t2=tk.Entry(w,width=40)
t3=tk.Entry(w,width=40)
t1.grid(row=0,column=1)
t2.grid(row=1,column=1)
t3.grid(row=2,column=1)
b1=tk.Button(text="Click Here")
b1.grid(row=3,column=1)
w.mainloop()
#How to Create Window and Handle Click Even on Button
import tkinter as tk
def f1(event):
print("Hello")
w=tk.Tk()
w.geometry('400x300')
b1=tk.Button(text="Click Here")
b1.grid(row=0,column=0)
b1.bind("<Button-1>",f1)
w.mainloop()
#Simple Example for Interaction with Text box
import tkinter as tk
def f1(event):
t1.insert(0,"Hello")
def f2(event):
t1.delete(0,'end')
root=tk.Tk()
root.geometry("400x400")
l1=tk.Label(root,text="Enter Somthing")
l2=tk.Label(root,text="Enter Somthing")
l3=tk.Label(root,text="Result")
t1=tk.Entry(root,width=30)
t2=tk.Entry(root,width=30)
t3=tk.Entry(root,width=30)
l1.grid(row=0,column=0)
t1.grid(row=0,column=1)
l2.grid(row=1,column=0)
t2.grid(row=1,column=1)
l3.grid(row=2,column=0)
t3.grid(row=2,column=1)
b1=tk.Button(root,text="Show Hello",width=15)
b2=tk.Button(root,text="Clear",width=15)
b1.grid(row=0,column=2)
b2.grid(row=1,column=2)
b1.bind("<Button-1>",f1)
b2.bind("<Button-1>",f2)
root.mainloop()
#Simple Example for Interaction with Text box by performing Addition,etc
import tkinter as tk
def f1(event):
s1=t1.get()
s2=t2.get()
a=int(s1)
b=int(s2)
c=a+b
t3.delete(0,'end')
t3.insert(0,c)
def f2(event):
t1.delete(0,'end')
t2.delete(0,'end')
t3.delete(0,'end')
root=tk.Tk()
root.geometry("400x400")
l1=tk.Label(root,text="1st Number")
l2=tk.Label(root,text="2nd Number")
l3=tk.Label(root,text="Result")
t1=tk.Entry(root,width=30)
t2=tk.Entry(root,width=30)
t3=tk.Entry(root,width=30)
l1.grid(row=0,column=0)
t1.grid(row=0,column=1)
l2.grid(row=1,column=0)
t2.grid(row=1,column=1)
l3.grid(row=2,column=0)
t3.grid(row=2,column=1)
b1=tk.Button(root,text="Addition",width=15)
b2=tk.Button(root,text="Clear",width=15)
b1.grid(row=0,column=2)
b2.grid(row=1,column=2)
b1.bind("<Button-1>",f1)
b2.bind("<Button-1>",f2)
root.mainloop()
#Calculator Code (Solution of Section C Question of AKTU Paper 2023)
import tkinter as tk
def b1Click(event):
s=t1.get()
s=s+"1"
t1.delete(0,'end')
t1.insert(0,s)
def b2Click(event):
s=t1.get()
s=s+"2"
t1.delete(0,'end')
t1.insert(0,s)
def b3Click(event):
s=t1.get()
s=s+"3"
t1.delete(0,'end')
t1.insert(0,s)
def b4Click(event):
s=t1.get()
s=s+"4"
t1.delete(0,'end')
t1.insert(0,s)
def b5Click(event):
s=t1.get()
s=s+"5"
t1.delete(0,'end')
t1.insert(0,s)
def b6Click(event):
s=t1.get()
s=s+"6"
t1.delete(0,'end')
t1.insert(0,s)
def b7Click(event):
s=t1.get()
s=s+"7"
t1.delete(0,'end')
t1.insert(0,s)
def b8Click(event):
s=t1.get()
s=s+"8"
t1.delete(0,'end')
t1.insert(0,s)
def b9Click(event):
s=t1.get()
s=s+"9"
t1.delete(0,'end')
t1.insert(0,s)
def b0Click(event):
s=t1.get()
s=s+"0"
t1.delete(0,'end')
t1.insert(0,s)
def baddClick(event):
s=t1.get()
s=s+"+"
t1.delete(0,'end')
t1.insert(0,s)
def bsubClick(event):
s=t1.get()
s=s+"-"
t1.delete(0,'end')
t1.insert(0,s)
def bmulClick(event):
s=t1.get()
s=s+"*"
t1.delete(0,'end')
t1.insert(0,s)
def bdivClick(event):
s=t1.get()
s=s+"/"
t1.delete(0,'end')
t1.insert(0,s)
def bequClick(event):
s=t1.get()
a=eval(s)
t1.delete(0,'end')
t1.insert(0,a)
def bacClick(event):
t1.delete(0,'end')
def bcClick(event):
s=t1.get()
s1=s[0:-1]
t1.delete(0,'end')
t1.insert(0,s1)
root=tk.Tk()
root.geometry("400x400")
t1=tk.Entry(root,width=60)
t1.grid(row=0,column=0,columnspan=5)
b1=tk.Button(root,text="1",width=8,height=3,bg='orange')
b1.grid(row=1,column=0)
b2=tk.Button(root,text="2",width=8,height=3)
b2.grid(row=1,column=1)
b3=tk.Button(root,text="3",width=8,height=3)
b3.grid(row=1,column=2)
bc=tk.Button(root,text="C",width=8,height=3)
bc.grid(row=1,column=3)
bac=tk.Button(root,text="AC",width=8,height=3)
bac.grid(row=1,column=4)
b4=tk.Button(root,text="4",width=8,height=3)
b4.grid(row=2,column=0)
b5=tk.Button(root,text="5",width=8,height=3)
b5.grid(row=2,column=1)
b6=tk.Button(root,text="6",width=8,height=3)
b6.grid(row=2,column=2)
badd=tk.Button(root,text="+",width=8,height=3)
badd.grid(row=2,column=3)
bsub=tk.Button(root,text="-",width=8,height=3)
bsub.grid(row=2,column=4)
b7=tk.Button(root,text="7",width=8,height=3)
b7.grid(row=3,column=0)
b8=tk.Button(root,text="8",width=8,height=3)
b8.grid(row=3,column=1)
b9=tk.Button(root,text="9",width=8,height=3)
b9.grid(row=3,column=2)
bmul=tk.Button(root,text="*",width=8,height=3)
bmul.grid(row=3,column=3)
bdiv=tk.Button(root,text="/",width=8,height=3)
bdiv.grid(row=3,column=4)
bpm=tk.Button(root,text="+/-",width=8,height=3)
bpm.grid(row=4,column=0)
b0=tk.Button(root,text="0",width=8,height=3)
b0.grid(row=4,column=1)
bdec=tk.Button(root,text=".",width=8,height=3)
bdec.grid(row=4,column=2)
bequ=tk.Button(root,text="=",width=18,height=3,fg='blue')
bequ.grid(row=4,column=3,columnspan=2)
b1.bind("<Button-1>",b1Click)
b2.bind("<Button-1>",b2Click)
b3.bind("<Button-1>",b3Click)
b4.bind("<Button-1>",b4Click)
b5.bind("<Button-1>",b5Click)
b6.bind("<Button-1>",b6Click)
b7.bind("<Button-1>",b7Click)
b8.bind("<Button-1>",b8Click)
b9.bind("<Button-1>",b9Click)
b0.bind("<Button-1>",b0Click)
badd.bind("<Button-1>",baddClick)
bsub.bind("<Button-1>",bsubClick)
bmul.bind("<Button-1>",bmulClick)
bdiv.bind("<Button-1>",bdivClick)
bequ.bind("<Button-1>",bequClick)
bac.bind("<Button-1>",bacClick)
bc.bind("<Button-1>",bcClick)
root.mainloop()

You might also like