Tuesday, 30 May 2017

searching techniques

Binary search:


def binarySearch(alist, item):
   first = 0
   last = len(alist)-1
   found = False

   while first<=last and not found:
       midpoint = (first + last)//2
       if alist[midpoint] == item:
           found = True
       else:
           if item < alist[midpoint]:
               last = midpoint-1
           else:
               first = midpoint+1

   return found

testlist = [0, 1, 2, 8, 13, 17, 19, 32, 42]
print(binarySearch(testlist, 3))
print(binarySearch(testlist, 13))


Sequential search:

def sequentialSearch(alist, item):
   pos = 0
   found = False

   while pos < len(alist) and not found:
       if alist[pos] == item:
           found = True
       else:
           pos = pos+1

   return found

testlist = [1, 2, 32, 8, 17, 19, 42, 13, 0]
print(sequentialSearch(testlist, 3))
print(sequentialSearch(testlist, 13))


Hashing:

class HashTable:
    def __init__(self):
        self.size = 11
        self.slots = [None] * self.size
        self.data = [None] * self.size

    def put(self,key,data):
      hashvalue = self.hashfunction(key,len(self.slots))

      if self.slots[hashvalue] == None:
        self.slots[hashvalue] = key
        self.data[hashvalue] = data
      else:
        if self.slots[hashvalue] == key:
          self.data[hashvalue] = data  #replace
        else:
          nextslot = self.rehash(hashvalue,len(self.slots))
          while self.slots[nextslot] != None and \
                          self.slots[nextslot] != key:
            nextslot = self.rehash(nextslot,len(self.slots))

          if self.slots[nextslot] == None:
            self.slots[nextslot]=key
            self.data[nextslot]=data
          else:
            self.data[nextslot] = data #replace

    def hashfunction(self,key,size):
         return key%size

    def rehash(self,oldhash,size):
        return (oldhash+1)%size

    def get(self,key):
      startslot = self.hashfunction(key,len(self.slots))

      data = None
      stop = False
      found = False
      position = startslot
      while self.slots[position] != None and  \
                           not found and not stop:
         if self.slots[position] == key:
           found = True
           data = self.data[position]
         else:
           position=self.rehash(position,len(self.slots))
           if position == startslot:
               stop = True
      return data

    def __getitem__(self,key):
        return self.get(key)

    def __setitem__(self,key,data):
        self.put(key,data)

H=HashTable()
H[54]="cat"
H[26]="dog"
H[93]="lion"
H[17]="tiger"
H[77]="bird"
H[31]="cow"
H[44]="goat"
H[55]="pig"
H[20]="chicken"
print(H.slots)
print(H.data)

print(H[20])

print(H[17])
H[20]='duck'
print(H[20])
print(H[99])

Sunday, 28 May 2017

GUI:Calculator program

import sys
from tkinter import *
def btnClearDisplay( ):
    global operator
    operator=" "
    input_num.set(" ")

def btnClick(numbers):
    global operator
    operator=operator +str(numbers)
    input_num.set(operator)

def btnEqual( ):
    global operator
    sumup=str(eval(operator))
    input_num.set(sumup)
    operator=" "
 
root=Tk( )
frame=Frame(root)
frame.pack()
root.title('calculater')
operator=" "
input_num=StringVar()
frame1=Frame(root)
frame1.pack(side=TOP)
txtDisplay=Entry(frame1,textvariable=input_num,bd=20,insertwidth=1,font=30)
txtDisplay.pack(side=TOP)
button1=Button(frame1,padx=16,pady=16,bd=8,text="1",fg="black",command=lambda:btnClick(1))
button1.pack(side=LEFT)
button2=Button(frame1,padx=16,pady=16,bd=8,text="2",fg="black",command=lambda:btnClick(2))
button2.pack(side=LEFT)
button3=Button(frame1,padx=16,pady=16,bd=8,text="3",fg="black",command=lambda:btnClick(3))
button3.pack(side=LEFT)
button4=Button(frame1,padx=16,pady=16,bd=8,text="4",fg="black",command=lambda:btnClick(4))
button4.pack(side=LEFT)

frame2=Frame(root)
frame2.pack(side=TOP)

button1=Button(frame2,padx=16,pady=16,bd=8,text="5",fg="black",command=lambda:btnClick(5))
button1.pack(side=LEFT)
button2=Button(frame2,padx=16,pady=16,bd=8,text="6",fg="black",command=lambda:btnClick(6))
button2.pack(side=LEFT)
button3=Button(frame2,padx=16,pady=16,bd=8,text="7",fg="black",command=lambda:btnClick(7))
button3.pack(side=LEFT)
button4=Button(frame2,padx=16,pady=16,bd=8,text="8",fg="black",command=lambda:btnClick(8))
button4.pack(side=LEFT)

frame3=Frame(root)
frame3.pack(side=TOP)

button1=Button(frame3,padx=16,pady=16,bd=8,text="9",fg="black",command=lambda:btnClick(9))
button1.pack(side=LEFT)
button2=Button(frame3,padx=16,pady=16,bd=8,text="0",fg="black",command=lambda:btnClick(0))
button2.pack(side=LEFT)
button3=Button(frame3,padx=16,pady=16,bd=8,text="c",fg="black",command=btnClearDisplay)
button3.pack(side=LEFT)
button4=Button(frame3,padx=16,pady=16,bd=8,text="=",fg="black",command=btnEqual)
button4.pack(side=LEFT)

frame4=Frame(root)
frame4.pack(side=TOP)

button1=Button(frame4,padx=16,pady=16,bd=8,text="*",fg="black",command=lambda:btnClick("*"))
button1.pack(side=LEFT)
button2=Button(frame4,padx=16,pady=16,bd=8,text="/",fg="black",command=lambda:btnClick("/"))
button2.pack(side=LEFT)
button3=Button(frame4,padx=16,pady=16,bd=8,text="-",fg="black",command=lambda:btnClick("-"))
button3.pack(side=LEFT)
button4=Button(frame4,padx=16,pady=16,bd=8,text="+",fg="black",command=lambda:btnClick("+"))
button4.pack(side=LEFT)


root.mainloop( )

Output:


fast-food restaurant program

from tkinter import *
import random
import time
import datetime
from tkinter import Tk,StringVar,ttk,messagebox

root=Tk()
root.geometry("1350x750+0+0")
root.title("Fast Food Restaurant")

tops=Frame(root,width=1350,height=100,bd=12,relief="raise")
tops.pack(side=TOP)
lbltitle=Label(tops,font=('arial',50,'bold'),text="\tFast Food Restaurant\t\t\t")
lbltitle.grid(row=0,column=0)

bottommainframe=Frame(root,width=1350,height=650,bd=12,relief="raise")
bottommainframe.pack(side=BOTTOM)

f1=Frame(bottommainframe,width=450,height=650,bd=12,relief="raise")
f1.pack(side=LEFT)
f2=Frame(bottommainframe,width=450,height=650,bd=12,relief="raise")
f2.pack(side=LEFT)
f2top=Frame(f2,width=450,height=350,bd=6,relief="raise")
f2top.pack(side=TOP)
f2bottom=Frame(f2,width=450,height=300,bd=6,relief="raise")
f2bottom.pack(side=BOTTOM)
f3=Frame(bottommainframe,width=450,height=650,bd=12,relief="raise")
f3.pack(side=RIGHT)

var1=IntVar()
var1.set(0)
var2=IntVar()
var2.set(0)
var3=IntVar()
var3.set(0)
var4=IntVar()
var4.set(0)
var5=IntVar()
var5.set(0)
var6=IntVar()
var6.set(0)
var7=IntVar()
var7.set(0)
var8=IntVar()
var8.set(0)
var9=IntVar()
var9.set(0)
var10=IntVar()
var10.set(0)
var11=IntVar()
var11.set(0)
var12=IntVar()
var12.set(0)
var13=IntVar()
var13.set(0)
var14=IntVar()
var14.set(0)
var15=IntVar()
var15.set(0)
var16=IntVar()
var16.set(0)
var17=IntVar()
var17.set(0)
var18=IntVar()
var18.set(0)
var19=IntVar()
var19.set(0)
var20=IntVar()
var20.set(0)
var21=IntVar()
var21.set(0)
var22=StringVar()
var22.set("")
var23=IntVar()
var23.set("")
varFries=StringVar()
varFries.set("0")
varSalad=StringVar()
varSalad.set("0")
varHamburger=StringVar()
varHamburger.set("0")
varOnionrings=StringVar()
varOnionrings.set("0")
varChickensalad=StringVar()
varChickensalad.set("0")
varFishsandwich=StringVar()
varFishsandwich.set("0")
varCheesesandwich=StringVar()
varCheesesandwich.set("0")
varChikensandwich=StringVar()
varChikensandwich.set("0")

varHashBrown=StringVar()
varHashBrown.set("0")
varToastedBagel=StringVar()
varToastedBagel.set("0")
varPancakesSyrup=StringVar()
varPancakesSyrup.set("0")
varPineappleStick=StringVar()
varPineappleStick.set("0")
varChacolateMuffin=StringVar()
varChacolateMuffin.set("0")

varTea=StringVar()
varTea.set("0")
varCola=StringVar()
varCola.set("0")
varCoffee=StringVar()
varCoffee.set("0")
varWaterBottle=StringVar()
varWaterBottle.set("0")
varOrange=StringVar()
varOrange.set("0")
varVanillaCone=StringVar()
varVanillaCone.set("0")
varVanillaShake=StringVar()
varVanillaShake.set("0")
varStrawberryShake=StringVar()
varStrawberryShake.set("0")

varChange=StringVar()
varChange.set("0")
varTax=StringVar()
varTax.set("0")
varPayment=IntVar()
varPayment.set(" ")

varSubTotal=StringVar()
varSubTotal.set("0")
varTotal=StringVar()
varTotal.set("0")

def iExit():
    qExit=messagebox.askyesno("Fast Food","Do You Want To Quit?")
    if qExit > 0:
        root.destroy()
        return

def Reset():
    varFries.set("0")
    varSalad.set("0")
    varHamburger.set("0")
    varOnionrings.set("0")
    varChickensalad.set("0")
    varFishsandwich.set("0")
    varCheesesandwich.set("0")
    varChikensandwich.set("0")
 
    varHashBrown.set("0")
    varToastedBagel.set("0")
    varPancakesSyrup.set("0")
    varPineappleStick.set("0")
    varChacolateMuffin.set("0")
 
    varTea.set("0")
    varCola.set("0")
    varCoffee.set("0")
    varWaterBottle.set("0")
    varOrange.set("0")
    varVanillaCone.set("0")
    varVanillaShake.set("0")
    varStrawberryShake.set("0")
    varPayment.set(" ")
    varChange.set("0")
    varTax.set("0")
    varSubTotal.set("0")
    varTotal.set("0")

    txtFries.configure(state=DISABLED)
    txtSalad.configure(state=DISABLED)
    txtHamburger.configure(state=DISABLED)
    txtOnionrings.configure(state=DISABLED)
    txtChickensalad.configure(state=DISABLED)
    txtFishsandwich.configure(state=DISABLED)
    txtCheesesandwich.configure(state=DISABLED)
    txtChikensandwich.configure(state=DISABLED)
 
    txtHashBrown.configure(state=DISABLED)
    txtToastedBagel.configure(state=DISABLED)
    txtPancakesSyrup.configure(state=DISABLED)
    txtPineappleStick.configure(state=DISABLED)
    txtChacolateMuffin.configure(state=DISABLED)

    txtTea.configure(state=DISABLED)
    txtCola.configure(state=DISABLED)
    txtCoffee.configure(state=DISABLED)
    txtWaterBottle.configure(state=DISABLED)
    txtOrange.configure(state=DISABLED)
    txtVanillaCone.configure(state=DISABLED)
    txtVanillaShake.configure(state=DISABLED)
    txtStrawberryShake.configure(state=DISABLED)

    txtChange.configure(state=DISABLED)
    txtTax.configure(state=DISABLED)
    #txtPayment.configure(state=DISABLED)
    txtSubTotal.configure(state=DISABLED)
    txtTotal.configure(state=DISABLED)


def checkFries():
    if(var1.get()==1):
        txtFries.configure(state=NORMAL)
        varFries.set("")
    elif(var1.get()==0):
        txtFries.configure(state=DISABLED)
        varFries.set("0")

def checkSalad():
    if(var2.get()==1):
        txtSalad.configure(state=NORMAL)
        varSalad.set("")
    elif(var2.get()==0):
        txtSalad.configure(state=DISABLED)
        varSalad.set("0")

def checkHamburger():
    if(var3.get()==1):
        txtHamburger.configure(state=NORMAL)
        varHamburger.set("")
    elif(var3.get()==0):
        txtHamburger.configure(state=DISABLED)
        varHamburger.set("0")

def checkOnionrings():
    if(var4.get()==1):
        txtOnionrings.configure(state=NORMAL)
        varOnionrings.set("")
    elif(var4.get()==0):
        txtOnionrings.configure(state=DISABLED)
        varOnionrings.set("0")

def checkChickensalad():
    if(var5.get()==1):
        txtChickensalad.configure(state=NORMAL)
        varChickensalad.set("")
    elif(var5.get()==0):
        txtChickensalad.configure(state=DISABLED)
        varChickensalad.set("0")

def checkFishsandwich():
    if(var6.get()==1):
        txtFishsandwich.configure(state=NORMAL)
        varFishsandwich.set("")
    elif(var6.get()==0):
        txtFishsandwich.configure(state=DISABLED)
        varFishsandwich.set("0")

def checkCheesesandwich():
    if(var7.get()==1):
        txtCheesesandwich.configure(state=NORMAL)
        varCheesesandwich.set("")
    elif(var7.get()==0):
        txtCheesesandwich.configure(state=DISABLED)
        varCheesesandwich.set("0")

def checkChikensandwich():
    if(var8.get()==1):
        txtChikensandwich.configure(state=NORMAL)
        varChikensandwich.set("")
    elif(var8.get()==0):
        txtChikensandwich.configure(state=DISABLED)
        varChikensandwich.set("0")

def checkHashBrown():
    if(var9.get()==1):
        txtHashBrown.configure(state=NORMAL)
        varHashBrown.set("")
    elif(var9.get()==0):
        txtHashBrown.configure(state=DISABLED)
        varHashBrown.set("0")

def checkToastedBagel():
    if(var10.get()==1):
        txtToastedBagel.configure(state=NORMAL)
        varToastedBagel.set("")
    elif(var10.get()==0):
        txtToastedBagel.configure(state=DISABLED)
        varToastedBagel.set("0")

def checkPancakesSyrup():
    if(var11.get()==1):
        txtPancakesSyrup.configure(state=NORMAL)
        varPancakesSyrup.set("")
    elif(var11.get()==0):
        txtPancakesSyrup.configure(state=DISABLED)
        varPancakesSyrup.set("0")

def checkPineappleStick():
    if(var12.get()==1):
        txtPineappleStick.configure(state=NORMAL)
        varPineappleStick.set("")
    elif(var12.get()==0):
        txtPineappleStick.configure(state=DISABLED)
        varPineappleStick.set("0")

def checkChacolateMuffin():
    if(var13.get()==1):
        txtChacolateMuffin.configure(state=NORMAL)
        varChacolateMuffin.set("")
    elif(var13.get()==0):
        txtChacolateMuffin.configure(state=DISABLED)
        varChacolateMuffin.set("0")

def checkTea():
    if(var14.get()==1):
        txtTea.configure(state=NORMAL)
        varTea.set("")
    elif(var14.get()==0):
        txtTea.configure(state=DISABLED)
        varTea.set("0")

def checkCola():
    if(var15.get()==1):
        txtCola.configure(state=NORMAL)
        varCola.set("")
    elif(var15.get()==0):
        txtCola.configure(state=DISABLED)
        varCola.set("0")

def checkCoffee():
    if(var16.get()==1):
        txtCoffee.configure(state=NORMAL)
        varCoffee.set("")
    elif(var16.get()==0):
        txtCoffee.configure(state=DISABLED)
        varCoffee.set("0")

def checkWaterBottle():
    if(var17.get()==1):
        txtWaterBottle.configure(state=NORMAL)
        varWaterBottle.set("")
    elif(var17.get()==0):
        txtWaterBottle.configure(state=DISABLED)
        varWaterBottle.set("0")

def checkOrange():
    if(var18.get()==1):
        txtOrange.configure(state=NORMAL)
        varOrange.set("")
    elif(var18.get()==0):
        txtOrange.configure(state=DISABLED)
        varOrange.set("0")

def checkVanillaCone():
    if(var19.get()==1):
        txtVanillaCone.configure(state=NORMAL)
        varVanillaCone.set("")
    elif(var19.get()==0):
        txtVanillaCone.configure(state=DISABLED)
        varVanillaCone.set("0")

def checkVanillaShake():
    if(var20.get()==1):
        txtVanillaShake.configure(state=NORMAL)
        varVanillaShake.set("")
    elif(var20.get()==0):
        txtVanillaShake.configure(state=DISABLED)
        varVanillaShake.set("0")
def checkStrawberryShake():
    if(var21.get()==1):
        txtStrawberryShake.configure(state=NORMAL)
        varStrawberryShake.set("")
    elif(var21.get()==0):
        txtStrawberryShake.configure(state=DISABLED)
        varStrawberryShake.set("0")

def costofmeal():
    meal1=float(varFries.get())
    meal2=float(varSalad.get())
    meal3=float(varHamburger.get())
    meal4=float(varOnionrings.get())
    meal5=float(varChickensalad.get())
    meal6=float(varFishsandwich.get())
    meal7=float(varCheesesandwich.get())
    meal8=float(varChikensandwich.get())
 
    meal9=float(varHashBrown.get())
    meal10=float(varToastedBagel.get())
    meal11=float(varPancakesSyrup.get())
    meal12=float(varPineappleStick.get())
    meal13=float(varChacolateMuffin.get())
 
    meal14=float(varTea.get())
    meal15=float(varCola.get())
    meal16=float(varCoffee.get())
    meal17=float(varWaterBottle.get())
    meal18=float(varOrange.get())
    meal19=float(varVanillaCone.get())
    meal20=float(varVanillaShake.get())
    meal21=float(varStrawberryShake.get())


    iCost=((meal1*50)+(meal2*60)+(meal3*70)+(meal4*65)+(meal5*100)+(meal6*130)+(meal7*100)+(meal8*120)
                 +(meal9*150)+(meal10*200)+(meal11*250)+(meal12*200)+(meal13*180)+(meal14*10)+(meal15*50)
                 +(meal16*20)+(meal17*30)+(meal18*40)+(meal19*80)+(meal20*70)+(meal21*110))


    if(var22.get()=="Cash"):
        Change=float(varChange.get())
        Cost=((meal1*50)+(meal2*60)+(meal3*70)+(meal4*65)+(meal5*100)+(meal6*130)+(meal7*100)+(meal8*120)
                 +(meal9*150)+(meal10*200)+(meal11*250)+(meal12*200)+(meal13*180)+(meal14*10)+(meal15*50)
                 +(meal16*20)+(meal17*30)+(meal18*40)+(meal19*80)+(meal20*70)+(meal21*110))
        Tax=(Cost+3.4)/100
        Taxq="Rs",str('%.2f'%(Tax))
        varTax.set(Taxq)

        Costq="Rs",str('%.2f'%(Cost))
        varSubTotal.set(Costq)
        Total="Rs",str('%.2f'%((Cost+Tax)))
        varTotal.set(Total)

        Change=(Change-(Cost+Tax))
        Changeq="Rs",str('%.2f'%(Change))
        varChange.set(Changeq)
    elif(var22.get()=="Master Card" or "Visa Card" or "Debit Card" or "Credit Card" or "Internet Banking"):
        varChange.set("")
        Cost=((meal1*50)+(meal2*60)+(meal3*70)+(meal4*65)+(meal5*100)+(meal6*130)+(meal7*100)+(meal8*120)
                 +(meal9*150)+(meal10*200)+(meal11*250)+(meal12*200)+(meal13*180)+(meal14*10)+(meal15*50)
                 +(meal16*20)+(meal17*30)+(meal18*40)+(meal19*80)+(meal20*70)+(meal21*110))
        Tax=(Cost+3.4)/100
        Taxq="Rs",str('%.2f'%(Tax))
        varTax.set(Taxq)
        Costq="Rs",str('%.2f'%(Cost))
        varSubTotal.set(Costq)
        Totalq="Rs",str('%.2f'%((Cost+Tax)))
        varTotal.set(Totalq)
        varChange.set("")
     
     
     
     

#-------------------------FRAME-1-----------------------------------
lbmeal=Label(f1,font=('arial',18,'bold'),text="Fast Food Meal and vegitarian",fg='red')
lbmeal.grid(row=0,column=0)

Fries=Checkbutton(f1,text="Fries\t\tRs.50",variable=var1,onvalue=1,offvalue=0,font=('arial',18,'bold'),command=checkFries).grid(row=1,column=0,sticky=W)
txtFries=Entry(f1,font=('arial',18,'bold'),textvariable=varFries,width=6,justify='right',state=DISABLED)
txtFries.grid(row=1,column=1)

Salad=Checkbutton(f1,text="Salad\t\tRs.60",variable=var2,onvalue=1,offvalue=0,font=('arial',18,'bold'),command=checkSalad).grid(row=2,column=0,sticky=W)
txtSalad=Entry(f1,font=('arial',18,'bold'),textvariable=varSalad,width=6,justify='right',state=DISABLED)
txtSalad.grid(row=2,column=1)

Hamburger=Checkbutton(f1,text="Hamburger\tRs.70",variable=var3,onvalue=1,offvalue=0,font=('arial',18,'bold'),command=checkHamburger).grid(row=3,column=0,sticky=W)
txtHamburger=Entry(f1,font=('arial',18,'bold'),textvariable=varHamburger,width=6,justify='right',state=DISABLED)
txtHamburger.grid(row=3,column=1)

Onionrings=Checkbutton(f1,text="Onionrings\tRs.65",variable=var4,onvalue=1,offvalue=0,font=('arial',18,'bold'),command=checkOnionrings).grid(row=4,column=0,sticky=W)
txtOnionrings=Entry(f1,font=('arial',18,'bold'),textvariable=varOnionrings,width=6,justify='right',state=DISABLED)
txtOnionrings.grid(row=4,column=1)

Chickensalad=Checkbutton(f1,text="Chickensalad\tRs.100",variable=var5,onvalue=1,offvalue=0,font=('arial',18,'bold'),command=checkChickensalad).grid(row=5,column=0,sticky=W)
txtChickensalad=Entry(f1,font=('arial',18,'bold'),textvariable=varChickensalad,width=6,justify='right',state=DISABLED)
txtChickensalad.grid(row=5,column=1)

lbsandwich=Label(f1,font=('arial',18,'bold'),text="Sandwich",fg='red')
lbsandwich.grid(row=6,column=0)

Fishsandwich=Checkbutton(f1,text="Fishsandwich\tRs.130",variable=var6,onvalue=1,offvalue=0,font=('arial',18,'bold'),command=checkFishsandwich).grid(row=7,column=0,sticky=W)
txtFishsandwich=Entry(f1,font=('arial',18,'bold'),textvariable=varFishsandwich,width=6,justify='right',state=DISABLED)
txtFishsandwich.grid(row=7,column=1)

Cheesesandwich=Checkbutton(f1,text="Cheesesandwich\tRs.100",variable=var7,onvalue=1,offvalue=0,font=('arial',18,'bold'),command=checkCheesesandwich).grid(row=8,column=0,sticky=W)
txtCheesesandwich=Entry(f1,font=('arial',18,'bold'),textvariable=varCheesesandwich,width=6,justify='right',state=DISABLED)
txtCheesesandwich.grid(row=8,column=1)

Chikensandwich=Checkbutton(f1,text="Chikensandwich\tRs.120",variable=var8,onvalue=1,offvalue=0,font=('arial',18,'bold'),command=checkChikensandwich).grid(row=9,column=0,sticky=W)
txtChikensandwich=Entry(f1,font=('arial',18,'bold'),textvariable=varChikensandwich,width=6,justify='right',state=DISABLED)
txtChikensandwich.grid(row=9,column=1)

lbSpace=Label(f1,text="\n\n\n\n\n\n\n\n\n\n")
lbSpace.grid(row=10,column=0)


#----------------------------FRAME-2----------------------------------
lbmeal=Label(f2top,font=('arial',18,'bold'),text="Deserts",fg='red')
lbmeal.grid(row=0,column=0)

HashBrown=Checkbutton(f2top,text="HashBrown\tRs.150",variable=var9,onvalue=1,offvalue=0,font=('arial',18,'bold'),command=checkHashBrown).grid(row=1,column=0,sticky=W)
txtHashBrown=Entry(f2top,font=('arial',18,'bold'),textvariable=varHashBrown,width=6,justify='right',state=DISABLED)
txtHashBrown.grid(row=1,column=1)

ToastedBagel=Checkbutton(f2top,text="ToastedBagel\tRs.200",variable=var10,onvalue=1,offvalue=0,font=('arial',18,'bold'),command=checkToastedBagel).grid(row=2,column=0,sticky=W)
txtToastedBagel=Entry(f2top,font=('arial',18,'bold'),textvariable=varToastedBagel,width=6,justify='right',state=DISABLED)
txtToastedBagel.grid(row=2,column=1)

PancakesSyrup=Checkbutton(f2top,text="PancakesSyrup\tRs.250",variable=var11,onvalue=1,offvalue=0,font=('arial',18,'bold'),command=checkPancakesSyrup).grid(row=3,column=0,sticky=W)
txtPancakesSyrup=Entry(f2top,font=('arial',18,'bold'),textvariable=varPancakesSyrup,width=6,justify='right',state=DISABLED)
txtPancakesSyrup.grid(row=3,column=1)

PineappleStick=Checkbutton(f2top,text="PineappleStick\tRs.200",variable=var12,onvalue=1,offvalue=0,font=('arial',18,'bold'),command=checkPineappleStick).grid(row=4,column=0,sticky=W)
txtPineappleStick=Entry(f2top,font=('arial',18,'bold'),textvariable=varPineappleStick,width=6,justify='right',state=DISABLED)
txtPineappleStick.grid(row=4,column=1)

ChacolateMuffin=Checkbutton(f2top,text="ChacolateMuffin\tRs.180",variable=var13,onvalue=1,offvalue=0,font=('arial',18,'bold'),command=checkChacolateMuffin).grid(row=5,column=0,sticky=W)
txtChacolateMuffin=Entry(f2top,font=('arial',18,'bold'),textvariable=varChacolateMuffin,width=6,justify='right',state=DISABLED)
txtChacolateMuffin.grid(row=5,column=1)


#-----------------------------------FRAME-3--------------------------

lbdrinks=Label(f3,font=('arial',18,'bold'),text="Drinks",fg='red')
lbdrinks.grid(row=0,column=0)

Tea=Checkbutton(f3,text="Tea\t\tRs.10",variable=var14,onvalue=1,offvalue=0,font=('arial',18,'bold'),command=checkTea).grid(row=1,column=0,sticky=W)
txtTea=Entry(f3,font=('arial',18,'bold'),textvariable=varTea,width=6,justify='right',state=DISABLED)
txtTea.grid(row=1,column=1)

Cola=Checkbutton(f3,text="Cola\t\tRs.50",variable=var15,onvalue=1,offvalue=0,font=('arial',18,'bold'),command=checkCola).grid(row=2,column=0,sticky=W)
txtCola=Entry(f3,font=('arial',18,'bold'),textvariable=varCola,width=6,justify='right',state=DISABLED)
txtCola.grid(row=2,column=1)

Coffee=Checkbutton(f3,text="Coffee\t\tRs.20",variable=var16,onvalue=1,offvalue=0,font=('arial',18,'bold'),command=checkCoffee).grid(row=3,column=0,sticky=W)
txtCoffee=Entry(f3,font=('arial',18,'bold'),textvariable=varCoffee,width=6,justify='right',state=DISABLED)
txtCoffee.grid(row=3,column=1)

WaterBottle=Checkbutton(f3,text="WaterBottle\tRs.30",variable=var17,onvalue=1,offvalue=0,font=('arial',18,'bold'),command=checkWaterBottle).grid(row=4,column=0,sticky=W)
txtWaterBottle=Entry(f3,font=('arial',18,'bold'),textvariable=varWaterBottle,width=6,justify='right',state=DISABLED)
txtWaterBottle.grid(row=4,column=1)

Orange=Checkbutton(f3,text="Orange\t\tRs.40",variable=var18,onvalue=1,offvalue=0,font=('arial',18,'bold'),command=checkOrange).grid(row=5,column=0,sticky=W)
txtOrange=Entry(f3,font=('arial',18,'bold'),textvariable=varOrange,width=6,justify='right',state=DISABLED)
txtOrange.grid(row=5,column=1)

lbShakes=Label(f3,font=('arial',18,'bold'),text="Shakes",fg='red')
lbShakes.grid(row=6,column=0)

VanillaCone=Checkbutton(f3,text="VanillaCone\tRs.80",variable=var19,onvalue=1,offvalue=0,font=('arial',18,'bold'),command=checkVanillaCone).grid(row=7,column=0,sticky=W)
txtVanillaCone=Entry(f3,font=('arial',18,'bold'),textvariable=varVanillaCone,width=6,justify='right',state=DISABLED)
txtVanillaCone.grid(row=7,column=1)

VanillaShake=Checkbutton(f3,text="VanillaShake\tRs.70",variable=var20,onvalue=1,offvalue=0,font=('arial',18,'bold'),command=checkVanillaShake).grid(row=8,column=0,sticky=W)
txtVanillaShake=Entry(f3,font=('arial',18,'bold'),textvariable=varVanillaShake,width=6,justify='right',state=DISABLED)
txtVanillaShake.grid(row=8,column=1)

StrawberryShake=Checkbutton(f3,text="StrawberryShake\tRs.110",variable=var21,onvalue=1,offvalue=0,font=('arial',18,'bold'),command=checkStrawberryShake).grid(row=9,column=0,sticky=W)
txtStrawberryShake=Entry(f3,font=('arial',18,'bold'),textvariable=varStrawberryShake,width=6,justify='right',state=DISABLED)
txtStrawberryShake.grid(row=9,column=1)

lbSpace=Label(f3,text="\n\n\n\n\n\n\n\n\n\n")
lbSpace.grid(row=10,column=0)

#-----------------------------------------------------------------------------------------------------------

lbPaymentMethod=Label(f2bottom,font=('arial',14,'bold'),text="Payment Method",bd=10,width=16,anchor='w')
lbPaymentMethod.grid(row=0,column=0)

lbChange=Label(f2bottom,font=('arial',14,'bold'),text="Change",bd=10,anchor='w')
lbChange.grid(row=0,column=1)
txtChange=Entry(f2bottom,font=('arial',14,'bold'),textvariable=varChange,width=6,state=DISABLED,justify='right')
txtChange.grid(row=0,column=2)

cmbPaymentMethod=ttk.Combobox(f2bottom,textvariable=var22,state="readonly",font=('arial',10,'bold'),width=20)
cmbPaymentMethod['value']=('Cash','Master Card','Visa Card','Debit Card','Credit Card','Internet Banking')
cmbPaymentMethod.current(0)
cmbPaymentMethod.grid(row=1,column=0)

lbTax=Label(f2bottom,font=('arial',14,'bold'),text="Tax",bd=10,anchor='w')
lbTax.grid(row=1,column=1)
txtTax=Entry(f2bottom,font=('arial',14,'bold'),textvariable=varTax,width=6,state=DISABLED,justify='right')
txtTax.grid(row=1,column=2)

txtPayment=Entry(f2bottom,font=('arial',18,'bold'),textvariable=varChange,width=6,state=DISABLED,justify='right')
txtPayment.grid(row=2,column=0)
lbSubTotal=Label(f2bottom,font=('arial',14,'bold'),text="SubTotal",bd=10,anchor='w')
lbSubTotal.grid(row=2,column=1)
txtSubTotal=Entry(f2bottom,font=('arial',14,'bold'),textvariable=varSubTotal,width=6,state=DISABLED,justify='right')
txtSubTotal.grid(row=2,column=2)

lbTotal=Label(f2bottom,font=('arial',14,'bold'),text="Total",bd=10,anchor='w')
lbTotal.grid(row=3,column=1)
txtTotal=Entry(f2bottom,font=('arial',14,'bold'),textvariable=varTotal,width=6,state=DISABLED,justify='right')
txtTotal.grid(row=3,column=2)
#---------------------------------------------

btnTotal=Button(f2bottom,padx=16,pady=1,bd=4,fg="black",font=('arial',16,'bold'),width=5,text="Total",command=costofmeal).grid(row=4,column=0)

btnReSet=Button(f2bottom,padx=16,pady=1,bd=4,fg="black",font=('arial',16,'bold'),width=5,text="ReSet",command=Reset).grid(row=4,column=1)

btnExit=Button(f2bottom,padx=16,pady=1,bd=4,fg="black",font=('arial',16,'bold'),width=5,text="Exit",command=lambda:iExit()).grid(row=4,column=2)

lbSpace=Label(f2bottom,text="\n\n\n\n\n\n\n")
lbSpace.grid(row=5,column=0)
             
             

root.mainloop()
           
Output:

Saturday, 27 May 2017

Restaurent Management System

from tkinter import *
import random
import time;
root=Tk()
root.geometry("1600x800+0+0")
root.title("Restaurant Management System")

text_Input=StringVar()
operator=" "

tops=Frame(root,width=1600,height=50,relief=SUNKEN)
tops.pack(side=TOP)
f1=Frame(root,width=800,height=700,relief=SUNKEN)
f1.pack(side=LEFT)
f2=Frame(root,width=300,height=700,relief=SUNKEN)
f2.pack(side=RIGHT)

#==============Time==========================
localtime=time.asctime(time.localtime(time.time()))

#==============INFORMATION=====================
lblinfo=Label(tops,font=('arial',50,'bold'),text="Siva Krishna Restaurant",fg="Steel Blue",bd=10,anchor='w')
lblinfo.grid(row=0,column=0)
lblinfo=Label(tops,font=('arial',20,'bold'),text=localtime,fg="Steel Blue",bd=10,anchor='w')
lblinfo.grid(row=1,column=0)

#=================calculator=======================
def buttonClick(numbers):
    global operator
    operator=operator + str(numbers)
    text_Input.set(operator)

def buttonClearDisplay():
    global operator
    operator=""
    text_Input.set("")

def buttonEqualClick():
    global operator
    res=str(eval(operator))
    text_Input.set(res)
    operator=""

def Ref():
    x=random.randint(10908,500876)
    randomRef=str(x)
    rand.set(randomRef)

    cof=float(Fries.get())
    cod=float(Drinks.get())
    cob=float(Burger.get())
    cofi=float(Filet.get())
    cocb=float(Chicken_Burger.get())
    cocheeseb=float(Cheese_Burger.get())

    costoffries=cof*0.99
    costofdrinks=cod*1.00
    costoffilet=cofi*2.99
    costofburger=cob*2.87
    costofchickenburger=cocb*2.89
    costofcheeseburger=cocheeseb*2.69

    cost="Rs",str('%.2f' %(costoffries + costofdrinks + costoffilet + costofburger + costofchickenburger + costofcheeseburger))
    paytax=((costoffries + costofdrinks + costoffilet + costofburger + costofchickenburger + costofcheeseburger)*0.2)
    subtotal=(costoffries + costofdrinks + costoffilet + costofburger + costofchickenburger + costofcheeseburger)
    ser_charge=((costoffries + costofdrinks + costoffilet + costofburger + costofchickenburger + costofcheeseburger)/99)
    service_charge="Rs",str('%.2f' %ser_charge)
    total="Rs",str('%.2f' %(paytax + ser_charge + subtotal))
    paidtax="Rs",str('%.2f' %paytax)
    Service_Charge.set(service_charge)
    Cost.set(cost)
    Tax.set(paidtax)
    SubTotal.set(subtotal)
    Total.set(total)
   
def Exit():
    root.destroy()

def Reset():
    rand.set(" ")
    Fries.set(" ")
    Burger.set(" ")
    Filet.set(" ")
    Drinks.set(" ")
    Chicken_Burger.set(" ")
    Cheese_Burger.set(" ")
    Cost.set(" ")
    Tax.set(" ")
    Service_Charge.set(" ")
    SubTotal.set(" ")
    Total.set(" ")
   
   
   


txtDisplay=Entry(f2,font=('arial',20,'bold'),textvariable=text_Input,bd=30,insertwidth=4,bg="powder blue",justify='right')
txtDisplay.grid(columnspan=4)

btn7=Button(f2,padx=16,pady=16,bd=8,fg="black",font=('arial',20,'bold'),text="7",bg="powder blue",command=lambda:buttonClick(7)).grid(row=2,column=0)
btn8=Button(f2,padx=16,pady=16,bd=8,fg="black",font=('arial',20,'bold'),text="8",bg="powder blue",command=lambda:buttonClick(8)).grid(row=2,column=1)
btn9=Button(f2,padx=16,pady=16,bd=8,fg="black",font=('arial',20,'bold'),text="9",bg="powder blue",command=lambda:buttonClick(9)).grid(row=2,column=2)
addition=Button(f2,padx=16,pady=16,bd=8,fg="black",font=('arial',20,'bold'),text="+",bg="powder blue",command=lambda:buttonClick("+")).grid(row=2,column=3)

btn4=Button(f2,padx=16,pady=16,bd=8,fg="black",font=('arial',20,'bold'),text="4",bg="powder blue",command=lambda:buttonClick(4)).grid(row=3,column=0)
btn5=Button(f2,padx=16,pady=16,bd=8,fg="black",font=('arial',20,'bold'),text="5",bg="powder blue",command=lambda:buttonClick(5)).grid(row=3,column=1)
btn6=Button(f2,padx=16,pady=16,bd=8,fg="black",font=('arial',20,'bold'),text="6",bg="powder blue",command=lambda:buttonClick(6)).grid(row=3,column=2)
subtraction=Button(f2,padx=16,pady=16,bd=8,fg="black",font=('arial',20,'bold'),text="-",bg="powder blue",command=lambda:buttonClick("-")).grid(row=3,column=3)

btn1=Button(f2,padx=16,pady=16,bd=8,fg="black",font=('arial',20,'bold'),text="1",bg="powder blue",command=lambda:buttonClick(1)).grid(row=4,column=0)
btn2=Button(f2,padx=16,pady=16,bd=8,fg="black",font=('arial',20,'bold'),text="2",bg="powder blue",command=lambda:buttonClick(2)).grid(row=4,column=1)
btn3=Button(f2,padx=16,pady=16,bd=8,fg="black",font=('arial',20,'bold'),text="3",bg="powder blue",command=lambda:buttonClick(3)).grid(row=4,column=2)
multiplication=Button(f2,padx=16,pady=16,bd=8,fg="black",font=('arial',20,'bold'),text="*",bg="powder blue",command=lambda:buttonClick("*")).grid(row=4,column=3)

btnClear=Button(f2,padx=16,pady=16,bd=8,fg="black",font=('arial',20,'bold'),text="c",bg="powder blue",command=buttonClearDisplay).grid(row=5,column=0)
btn0=Button(f2,padx=16,pady=16,bd=8,fg="black",font=('arial',20,'bold'),text="0",bg="powder blue",command=lambda:buttonClick(0)).grid(row=5,column=1)
btnEqual=Button(f2,padx=16,pady=16,bd=8,fg="black",font=('arial',20,'bold'),text="=",bg="powder blue",command=buttonEqualClick).grid(row=5,column=2)
divison=Button(f2,padx=16,pady=16,bd=8,fg="black",font=('arial',20,'bold'),text="/",bg="powder blue",command=lambda:buttonClick("/")).grid(row=5,column=3)

#==================================RESTAURANT MENU================================================
rand=StringVar()
Fries=StringVar()
Burger=StringVar()
Filet=StringVar()
Drinks=StringVar()
Chicken_Burger=StringVar()
Cheese_Burger=StringVar()
Cost=StringVar()
Tax=StringVar()
Service_Charge=StringVar()
SubTotal=StringVar()
Total=StringVar()

lblReference=Label(f1,font=('arial',16,'bold'),text="Reference",bd=16,anchor='w')
lblReference.grid(row=0,column=0)
txtReference=Entry(f1,font=('arial',16,'bold'),textvariable=rand,bd=10,insertwidth=4,bg="powder blue",justify="right")
txtReference.grid(row=0,column=1)

lblFries=Label(f1,font=('arial',16,'bold'),text="Fries",bd=16,anchor='w')
lblFries.grid(row=1,column=0)
txtFries=Entry(f1,font=('arial',16,'bold'),textvariable=Fries,bd=10,insertwidth=4,bg="powder blue",justify="right")
txtFries.grid(row=1,column=1)

lblBurger=Label(f1,font=('arial',16,'bold'),text="Burger",bd=16,anchor='w')
lblBurger.grid(row=2,column=0)
txtBurger=Entry(f1,font=('arial',16,'bold'),textvariable=Burger,bd=10,insertwidth=4,bg="powder blue",justify="right")
txtBurger.grid(row=2,column=1)

lblFilet=Label(f1,font=('arial',16,'bold'),text="Filet",bd=16,anchor='w')
lblFilet.grid(row=3,column=0)
txtFilet=Entry(f1,font=('arial',16,'bold'),textvariable=Filet,bd=10,insertwidth=4,bg="powder blue",justify="right")
txtFilet.grid(row=3,column=1)

lblDrinks=Label(f1,font=('arial',16,'bold'),text="Drinks",bd=16,anchor='w')
lblDrinks.grid(row=4,column=0)
txtDrinks=Entry(f1,font=('arial',16,'bold'),textvariable=Drinks,bd=10,insertwidth=4,bg="powder blue",justify="right")
txtDrinks.grid(row=4,column=1)

lblChicken_Burger=Label(f1,font=('arial',16,'bold'),text="Chicken_Burger",bd=16,anchor='w')
lblChicken_Burger.grid(row=5,column=0)
txtChicken_Burger=Entry(f1,font=('arial',16,'bold'),textvariable=Chicken_Burger,bd=10,insertwidth=4,bg="powder blue",justify="right")
txtChicken_Burger.grid(row=5,column=1)

lblCheese_Burger=Label(f1,font=('arial',16,'bold'),text="Cheese_Burger",bd=16,anchor='w')
lblCheese_Burger.grid(row=6,column=0)
txtCheese_Burger=Entry(f1,font=('arial',16,'bold'),textvariable=Cheese_Burger,bd=10,insertwidth=4,bg="powder blue",justify="right")
txtCheese_Burger.grid(row=6,column=1)

lblCost=Label(f1,font=('arial',16,'bold'),text="Cost",bd=16,anchor='w')
lblCost.grid(row=2,column=2)
txtCost=Entry(f1,font=('arial',16,'bold'),textvariable=Cost,bd=10,insertwidth=4,bg="powder blue",justify="right")
txtCost.grid(row=2,column=3)

lblTax=Label(f1,font=('arial',16,'bold'),text="Tax",bd=16,anchor='w')
lblTax.grid(row=3,column=2)
txtTax=Entry(f1,font=('arial',16,'bold'),textvariable=Tax,bd=10,insertwidth=4,bg="powder blue",justify="right")
txtTax.grid(row=3,column=3)

lblService_Charge=Label(f1,font=('arial',16,'bold'),text="Service_Charge",bd=16,anchor='w')
lblService_Charge.grid(row=4,column=2)
txtService_Charge=Entry(f1,font=('arial',16,'bold'),textvariable=Service_Charge,bd=10,insertwidth=4,bg="powder blue",justify="right")
txtService_Charge.grid(row=4,column=3)

lblSubTotal=Label(f1,font=('arial',16,'bold'),text="SubTotal",bd=16,anchor='w')
lblSubTotal.grid(row=5,column=2)
txtSubTotal=Entry(f1,font=('arial',16,'bold'),textvariable=SubTotal,bd=10,insertwidth=4,bg="powder blue",justify="right")
txtSubTotal.grid(row=5,column=3)

lblTotal=Label(f1,font=('arial',16,'bold'),text="Total",bd=16,anchor='w')
lblTotal.grid(row=6,column=2)
txtTotal=Entry(f1,font=('arial',16,'bold'),textvariable=Total,bd=10,insertwidth=4,bg="powder blue",justify="right")
txtTotal.grid(row=6,column=3)

#==============Buttons========================
btnTotal=Button(f1,padx=16,pady=8,bd=16,fg="black",font=('arial',16,'bold'),width=10,text="Total",bg="powder blue",command=Ref).grid(row=7,column=1)
btnReset=Button(f1,padx=16,pady=8,bd=16,fg="black",font=('arial',16,'bold'),width=10,text="Reset",bg="powder blue",command=Reset).grid(row=7,column=2)
btnExit=Button(f1,padx=16,pady=8,bd=16,fg="black",font=('arial',16,'bold'),width=10,text="Exit",bg="powder blue",command=Exit).grid(row=7,column=3)



root.mainloop()

Python GUI Programming:

Python provides various options for developing graphical user interfaces (GUI's). 

  • Tkinter
  • PyQt
  • WxPython

Tkinter Programming:

  • Tkinter is the standard GUI library for Python. 
  • Python when combined with Tkinter provides a fast and easy way to create GUI applications. 
  • In-order to work with Tkinter, first we are install Tkinter module.

C:\Python27\Scripts>pip install tkinter


  • After install that module, next we are import that module, how to import that module.
  • in python2.7(python2.x)

>>> import tkinter
>>> 
                                    (or)
>>> import Tkinter
>>> 
  • in python3.5(python3.x)

>>> import tkinter
>>> 
                                     (or)

>>> import Tkinter
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    import Tkinter
ImportError: No module named 'Tkinter'

example1:

import Tkinter
top = Tkinter.Tk( )

# Code to add widgets will go here...

top.mainloop()

output:


Example2: button: The Button widget is used to add buttons in a Python application. These buttons can display text or images that convey the purpose of the buttons. You can attach a function or a method to a button which is called automatically when you click the button

import Tkinter
import tkMessageBox

top = Tkinter.Tk( )

def helloCallBack( ):
       tkMessageBox.showinfo( "Hello Python", "Hello World")

B = Tkinter.Button(top, text ="Hello", command = helloCallBack)

B.pack( )
top.mainloop( )

Output:

Example3: canvas: The Canvas is a rectangular area intended for drawing pictures or other complex layouts. You can place graphics, text, widgets or frames on a Canvas.

import Tkinter
import tkMessageBox

top = Tkinter.Tk( )

C = Tkinter.Canvas(top, bg="blue", height=250, width=300)

coord = 10, 50, 240, 210
arc = C.create_arc(coord, start=0, extent=150, fill="red")

C.pack( )
top.mainloop( )

Output:


Example4: check button: The Checkbutton widget is used to display a number of options to a user as toggle buttons. The user can then select one or more options by clicking the button corresponding to each option.

from Tkinter import *
import tkMessageBox
import Tkinter

top = Tkinter.Tk( )
CheckVar1 = IntVar( )
CheckVar2 = IntVar( )
C1 = Checkbutton(top, text = "Music", variable = CheckVar1, \
                 onvalue = 1, offvalue = 0, height=5, \
                 width = 20)
C2 = Checkbutton(top, text = "Video", variable = CheckVar2, \
                 onvalue = 1, offvalue = 0, height=5, \
                 width = 20)
C1.pack( )
C2.pack( )
top.mainloop()

Output:




example5: entry field:The Entry widget is used to accept single-line text strings from a user.

from Tkinter import *

top = Tk( )
L1 = Label(top, text="User Name")
L1.pack( side = LEFT)
E1 = Entry(top, bd =5)

E1.pack(side = RIGHT)

top.mainloop( )

Output:


Example6: Frame: The Frame widget is very important for the process of grouping and organizing other widgets in a somehow friendly way.

from Tkinter import *

root = Tk()
frame = Frame(root)
frame.pack()

bottomframe = Frame(root)
bottomframe.pack( side = BOTTOM )

redbutton = Button(frame, text="Red", fg="red")
redbutton.pack( side = LEFT)

greenbutton = Button(frame, text="Brown", fg="brown")
greenbutton.pack( side = LEFT )

bluebutton = Button(frame, text="Blue", fg="blue")
bluebutton.pack( side = LEFT )

blackbutton = Button(bottomframe, text="Black", fg="black")
blackbutton.pack( side = BOTTOM)

root.mainloop( )

Output:


Example7: label: This widget implements a display box where you can place text or images. The text displayed by this widget can be updated at any time you want.

from Tkinter import *

root = Tk( )

var = StringVar()
label = Label( root, textvariable=var, relief=RAISED )

var.set("Hey!? How are you doing?")
label.pack( )
root.mainloop( )

Output:


Example8: Listbox: The Listbox widget is used to display a list of items from which a user can select a number of items.

from Tkinter import *
import tkMessageBox
import Tkinter

top = Tk()

Lb1 = Listbox(top)
Lb1.insert(1, "Python")
Lb1.insert(2, "Perl")
Lb1.insert(3, "C")
Lb1.insert(4, "PHP")
Lb1.insert(5, "JSP")
Lb1.insert(6, "Ruby")

Lb1.pack( )
top.mainloop( )

Output:


Example9: menubutton: A menubutton is the part of a drop-down menu that stays on the screen all the time. Every menubutton is associated with a Menu widget that can display the choices for that menubutton when the user clicks on it.

from Tkinter import *
import tkMessageBox
import Tkinter

top = Tk()

mb=  Menubutton ( top, text="condiments", relief=RAISED )
mb.grid()
mb.menu  =  Menu ( mb, tearoff = 0 )
mb["menu"]  =  mb.menu
    
mayoVar  = IntVar( )
ketchVar = IntVar( )

mb.menu.add_checkbutton ( label="mayo",
                          variable=mayoVar )
mb.menu.add_checkbutton ( label="ketchup",
                          variable=ketchVar )

mb.pack( )
top.mainloop( )

Output:


Example10: menu: The goal of this widget is to allow us to create all kinds of menus that can be used by our applications. The core functionality provides ways to create three menu types: pop-up, top-level and pull-down.

from Tkinter import *
def donothing( ):
   filewin = Toplevel(root)
   button = Button(filewin, text="Do nothing button")
   button.pack( )
   
root = Tk( )
menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="New", command=donothing)
filemenu.add_command(label="Open", command=donothing)
filemenu.add_command(label="Save", command=donothing)
filemenu.add_command(label="Save as...", command=donothing)
filemenu.add_command(label="Close", command=donothing)

filemenu.add_separator( )

filemenu.add_command(label="Exit", command=root.quit)
menubar.add_cascade(label="File", menu=filemenu)
editmenu = Menu(menubar, tearoff=0)
editmenu.add_command(label="Undo", command=donothing)

editmenu.add_separator()

editmenu.add_command(label="Cut", command=donothing)
editmenu.add_command(label="Copy", command=donothing)
editmenu.add_command(label="Paste", command=donothing)
editmenu.add_command(label="Delete", command=donothing)
editmenu.add_command(label="Select All", command=donothing)

menubar.add_cascade(label="Edit", menu=editmenu)
helpmenu = Menu(menubar, tearoff=0)
helpmenu.add_command(label="Help Index", command=donothing)
helpmenu.add_command(label="About...", command=donothing)
menubar.add_cascade(label="Help", menu=helpmenu)

root.config(menu=menubar)
root.mainloop( )

Output:





Example11: message:This widget provides a multiline and noneditable object that displays texts, automatically breaking lines and justifying their contents.

from Tkinter import *

root = Tk( )

var = StringVar( )
label = Message( root, textvariable=var, relief=RAISED )

var.set("Hey!? How are you doing?")
label.pack( )
root.mainloop( )

Output:


Example12: Radiobutton: This widget implements a multiple-choice button, which is a way to offer many possible selections to the user and lets user choose only one of them.

from Tkinter import *

def sel( ):
   selection = "You selected the option " + str(var.get())
   label.config(text = selection)

root = Tk( )
var = IntVar( )
R1 = Radiobutton(root, text="Option 1", variable=var, value=1,
                  command=sel)
R1.pack( anchor = W )

R2 = Radiobutton(root, text="Option 2", variable=var, value=2,
                  command=sel)
R2.pack( anchor = W )

R3 = Radiobutton(root, text="Option 3", variable=var, value=3,
                  command=sel)
R3.pack( anchor = W)

label = Label(root)
label.pack( )
root.mainloop( )

Output:



Example13: Scale:The Scale widget provides a graphical slider object that allows you to select values from a specific scale.

from Tkinter import *

def sel( ):
   selection = "Value = " + str(var.get())
   label.config(text = selection)

root = Tk( )
var = DoubleVar()
scale = Scale( root, variable = var )
scale.pack(anchor=CENTER)

button = Button(root, text="Get Scale Value", command=sel)
button.pack(anchor=CENTER)

label = Label(root)
label.pack( )

root.mainloop( )

Output:



Example14: Scrollbar: This widget provides a slide controller that is used to implement vertical scrolled widgets, such as Listbox, Text and Canvas. Note that you can also create horizontal scrollbars on Entry widgets.

from Tkinter import *

root = Tk()
scrollbar = Scrollbar(root)
scrollbar.pack( side = RIGHT, fill=Y )

mylist = Listbox(root, yscrollcommand = scrollbar.set )
for line in range(100):
   mylist.insert(END, "This is line number " + str(line))

mylist.pack( side = LEFT, fill = BOTH )
scrollbar.config( command = mylist.yview )

mainloop( )

Output:



Example15: Text: Text widgets provide advanced capabilities that allow you to edit a multiline text and format the way it has to be displayed, such as changing its color and font.

from Tkinter import *

def onclick():
   pass

root = Tk()
text = Text(root)
text.insert(INSERT, "Hello.....")
text.insert(END, "Bye Bye.....")
text.pack()

text.tag_add("here", "1.0", "1.4")
text.tag_add("start", "1.8", "1.13")
text.tag_config("here", background="yellow", foreground="blue")
text.tag_config("start", background="black", foreground="green")
root.mainloop( )

Output:

Example16: Top-Level: Toplevel widgets work as windows that are directly managed by the window manager. They do not necessarily have a parent widget on top of them.
Your application can use any number of top-level windows.
from Tkinter import *

root = Tk( )
top = Toplevel( )

top.mainloop( )

Output:


Example17: Spinbox: The Spinbox widget is a variant of the standard Tkinter Entry widget, which can be used to select from a fixed number of values.

from Tkinter import *

master = Tk( )

w = Spinbox(master, from_=0, to=10)
w.pack( )

mainloop( )

Output:





Example18:PanedWindow: A PanedWindow is a container widget that may contain any number of panes, arranged horizontally or vertically.

from Tkinter import *

m1 = PanedWindow()
m1.pack(fill=BOTH, expand=1)

left = Label(m1, text="left pane")
m1.add(left)

m2 = PanedWindow(m1, orient=VERTICAL)
m1.add(m2)

top = Label(m2, text="top pane")
m2.add(top)

bottom = Label(m2, text="bottom pane")
m2.add(bottom)

mainloop( )

Output:


Example19: LabelFrame: A labelframe is a simple container widget. Its primary purpose is to act as a spacer or container for complex window layouts.

from Tkinter import *

root = Tk( )

labelframe = LabelFrame(root, text="This is a LabelFrame")
labelframe.pack(fill="both", expand="yes")
 
left = Label(labelframe, text="Inside the LabelFrame")
left.pack( )
 
root.mainloop( )

Output:



Example20: tkMessageBox: The tkMessageBox module is used to display message boxes in your applications. This module provides a number of functions that you can use to display an appropriate message.

import Tkinter
import tkMessageBox

top = Tkinter.Tk()
def hello():
   tkMessageBox.showinfo("Say Hello", "Hello World")

B1 = Tkinter.Button(top, text = "Say Hello", command = hello)
B1.pack()

top.mainloop()

Output: