Advertisement
here2share

# TkPhoneListDemo.py

Jun 5th, 2015
368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.38 KB | None | 0 0
  1. # TkPhoneListDemo.py
  2.  
  3. from Tkinter import *
  4.  
  5. def whichSelected () :
  6.     print "At %s of %d" % (select.curselection(), len(phonelist))
  7.     print phonelist
  8.     return int(select.curselection()[0])
  9.  
  10. def addEntry () :
  11.     if '' not in [nameVar.get(),phoneVar.get()]:
  12.         phonelist.append ([nameVar.get(), phoneVar.get()])
  13.         setSelect ()
  14.  
  15. def updateEntry() :
  16.     if '' not in [nameVar.get(),phoneVar.get()]:
  17.         phonelist[whichSelected()] = [nameVar.get(), phoneVar.get()]
  18.         setSelect ()
  19.  
  20. def deleteEntry() :
  21.     try:
  22.         del phonelist[whichSelected()]
  23.         setSelect ()
  24.     except: pass
  25.  
  26. def loadEntry  () :
  27.     name, phone = phonelist[whichSelected()]
  28.     nameVar.set(name)
  29.     phoneVar.set(phone)
  30.  
  31. def makeWindow () :
  32.     global nameVar, phoneVar, select
  33.     win = Tk()
  34.  
  35.     frame1 = Frame(win)
  36.     frame1.pack()
  37.  
  38.     Label(frame1, text="Name").grid(row=0, column=0, sticky=W)
  39.     nameVar = StringVar()
  40.     name = Entry(frame1, textvariable=nameVar)
  41.     name.grid(row=0, column=1, sticky=W)
  42.  
  43.     Label(frame1, text="Phone").grid(row=1, column=0, sticky=W)
  44.     phoneVar= StringVar()
  45.     phone= Entry(frame1, textvariable=phoneVar)
  46.     phone.grid(row=1, column=1, sticky=W)
  47.  
  48.     frame2 = Frame(win)    # Row of buttons
  49.     frame2.pack()
  50.     b1 = Button(frame2,text=" Add  ",command=addEntry)
  51.     b2 = Button(frame2,text="Update",command=updateEntry)
  52.     b3 = Button(frame2,text="Delete",command=deleteEntry)
  53.     b4 = Button(frame2,text=" Load ",command=loadEntry)
  54.     b1.pack(side=LEFT); b2.pack(side=LEFT)
  55.     b3.pack(side=LEFT); b4.pack(side=LEFT)
  56.  
  57.     frame3 = Frame(win)    # select of names
  58.     frame3.pack()
  59.     scroll = Scrollbar(frame3, orient=VERTICAL)
  60.     select = Listbox(frame3, yscrollcommand=scroll.set, height=10)
  61.     scroll.config (command=select.yview)
  62.     scroll.pack(side=RIGHT, fill=Y)
  63.     select.pack(side=LEFT,  fill=BOTH, expand=1)
  64.     return win
  65.  
  66. def setSelect () :
  67.     phonelist.sort()
  68.     select.delete(0,END)
  69.     for name,phone in phonelist :
  70.         select.insert (END, name)
  71.  
  72. phonelist=[['1st Class Restaurant', '500-555-0001'], ['AAA', '1-800-555-0222'], ['Chinese Takeout', '500-555-7423'], ['Cruise Line Tickets', '500-555-7245'], ['Delux Danceclub', '500-555-3338'], ['Goddess Of Love', '500-555-7399'], ['Grandparents', '500-555-2274'], ['Joe Average', '500-555-5555'], ['John Doe', '500-555-0000'], ['Landlord', '500-555-7368'], ['Local Pizza', '500-555-6666'], ['Mom And Dad', '500-555-5683'], ['Smart Taxi', '500-555-8294']]
  73. win = makeWindow()
  74. setSelect ()
  75. win.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement