Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # TkPhoneListDemo.py
- from Tkinter import *
- def whichSelected () :
- print "At %s of %d" % (select.curselection(), len(phonelist))
- print phonelist
- return int(select.curselection()[0])
- def addEntry () :
- if '' not in [nameVar.get(),phoneVar.get()]:
- phonelist.append ([nameVar.get(), phoneVar.get()])
- setSelect ()
- def updateEntry() :
- if '' not in [nameVar.get(),phoneVar.get()]:
- phonelist[whichSelected()] = [nameVar.get(), phoneVar.get()]
- setSelect ()
- def deleteEntry() :
- try:
- del phonelist[whichSelected()]
- setSelect ()
- except: pass
- def loadEntry () :
- name, phone = phonelist[whichSelected()]
- nameVar.set(name)
- phoneVar.set(phone)
- def makeWindow () :
- global nameVar, phoneVar, select
- win = Tk()
- frame1 = Frame(win)
- frame1.pack()
- Label(frame1, text="Name").grid(row=0, column=0, sticky=W)
- nameVar = StringVar()
- name = Entry(frame1, textvariable=nameVar)
- name.grid(row=0, column=1, sticky=W)
- Label(frame1, text="Phone").grid(row=1, column=0, sticky=W)
- phoneVar= StringVar()
- phone= Entry(frame1, textvariable=phoneVar)
- phone.grid(row=1, column=1, sticky=W)
- frame2 = Frame(win) # Row of buttons
- frame2.pack()
- b1 = Button(frame2,text=" Add ",command=addEntry)
- b2 = Button(frame2,text="Update",command=updateEntry)
- b3 = Button(frame2,text="Delete",command=deleteEntry)
- b4 = Button(frame2,text=" Load ",command=loadEntry)
- b1.pack(side=LEFT); b2.pack(side=LEFT)
- b3.pack(side=LEFT); b4.pack(side=LEFT)
- frame3 = Frame(win) # select of names
- frame3.pack()
- scroll = Scrollbar(frame3, orient=VERTICAL)
- select = Listbox(frame3, yscrollcommand=scroll.set, height=10)
- scroll.config (command=select.yview)
- scroll.pack(side=RIGHT, fill=Y)
- select.pack(side=LEFT, fill=BOTH, expand=1)
- return win
- def setSelect () :
- phonelist.sort()
- select.delete(0,END)
- for name,phone in phonelist :
- select.insert (END, name)
- 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']]
- win = makeWindow()
- setSelect ()
- win.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement