from tkinter import * import random #for random colour backgrounds def fr(w,h,b,x,y): Frame(r,width=w,height=h,bg=b).place(x=x,y=y) def lab(t,b,f,x,y): Label(r,text=t,bg=b,fg=f).place(x=x,y=y) def bu(tex,cc,xx,yy): Button(r,text=tex,bg='orange',fg='black',bd=7,command=cc).place(x=int(xx),y=int(yy),width=110,height=110) col=['pink','cyan','blue','navy','red','yellow','orange','grey','black','white','green','gold'] r=Tk() r.geometry('400x500') #main part of calculator def cal(): fr(1200,2000,random.choice(col),0,0) fr(620,600,random.choice(col),50,470) fr(400,50,'pink',100,320) e1=Entry(r,bg='yellow') e1.place(x=100,y=60,height=70,width=400) e2=Entry(r,bg='yellow') e2.place(x=100,y=160,height=70,width=400) def o(): O['text']='Enter valid number' def os(): O['text']='Enter valid numbers' def sum(): try: O['text']=float(e1.get())+float(e2.get()) except: os() bu('+',sum,'80','500') def sub(): try: O['text']=float(e1.get())-float(e2.get()) except: os() bu('-',sub,'230','500') def mul(): try: O['text']=float(e1.get())*float(e2.get()) except: os() bu('*',mul,'380','500') def div(): try: O['text']=float(e1.get())/float(e2.get()) except: os() bu('/',div,'530','500') def sq(): try: O['text']=float(e1.get())**2 except: o() bu('x²',sq,'80','650') def sqrt(): try: O['text']=float(e1.get())**0.5 except: o() bu('√',sqrt,'230','650') def pow(): try: O['text']=float(e1.get())**float(e2.get()) except: os() bu('x^a',pow,'380','650') def powrt(): try: O['text']=float(e1.get())**(1/float(e2.get())) except: os() bu('x√',powrt,'530','650') def dice(): O['text']=random.randint(1,6) bu('DICE',dice,'80','800') def toss(): qq=random.randint(1,2) if qq==1: O['text']='HEADS' elif qq==2: O['text']='TAILS' bu('TOSS',toss,'230','800') def rand(): try: O['text']=random.randint(int(e1.get()),int(e2.get())) except: O['text']='enter valid numbers (num1