943,947 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 1773
  • Python RSS
Apr 30th, 2007
0

Tkinter Addressbook

Expand Post »
I am new to python and Tkinter. I was trying to build an addressbook for my class. I have the Tkinter GUI looking good - nothing fancy. Now I can't get my python part working. I want to keep the GUI in its own class and file. My two files are project.py and AddressBookGUI.py
AddressBookGUI.py
Python Syntax (Toggle Plain Text)
  1. # This file is named AddressBookGui.py
  2. from Tkinter import *
  3. import tkMessageBox
  4. from ScrolledText import *
  5. import project
  6.  
  7. class GUI:
  8. def __init__(self):
  9. root = Tk()
  10. root.title("Simple Address Book")
  11. root.geometry('500x400')
  12. b =Frame(root, bd = 3, bg='darkblue')
  13. b.pack(side='top', fill = 'x')
  14. findit = Entry(b, width=30)
  15. find = Button(b, text = "SEARCH LASTNAME",command=project.findname)
  16. find.pack(side = "right", pady = 5, padx = 10)
  17. findit.pack(side='right')
  18. record = Button(b, text ="NEW RECORD",command=project.openwindow)
  19. record.pack(side = "left", pady = 10, padx = 10)
  20. a = Frame(root, bd = 3,bg='darkblue')
  21. a.pack(side='left', fill = 'y')
  22. openfile = Button(a, text = "OPEN",command=project.readfile)
  23. openfile.pack(side = "top", pady =10, padx =10)
  24. printrec= Button(a, text ="PRINT LIST",command=project.printfile(thelist1))
  25. printrec.pack(pady = 40, padx = 15)
  26. helpfile = Button(a, text = "HELP",command=project.helpfile)
  27. helpfile.pack(side = "bottom", pady =20, padx =10)
  28. close = Button(a, text = "EXIT",command=project.closefile)
  29. close.pack(side = 'bottom',pady = 10, padx = 10)
  30. c=Frame(root,bd=3,bg='darkblue')
  31. c.pack(side='bottom',fill='x')
  32. deleteit = Entry(c, width=30)
  33. delete = Button(c, text = "DELETE NAME",command=project.deletefile)
  34. delete.pack(side="right",pady = 25, padx = 10)
  35. deleteit.pack(side='right')
  36. c = Frame(root, bd = 3, relief = 'groove')
  37. c.pack(side='top', fill = 'x')
  38. i =Frame(root)
  39. thelist1=Listbox(i,bg = 'white')
  40. thelist1.pack(fill = 'both')
  41. i.pack(side = 'top',fill = 'both',expand = 1)
  42. thetext = ScrolledText(i)
  43. thetext.pack(side = 'top',fill = 'both',expand =1)
  44. root.mainloop()
  45. def dialog(frame):
  46. win = Toplevel()
  47. win.geometry('400x230+100+100')
  48. d = Frame(win)
  49. txtfirst = Entry(d, width =40)
  50. fname = Label(d, width = 15,text = 'First Name')
  51. fname.pack(side = 'left',pady = 8)
  52. txtfirst.pack(side = 'left')
  53. d.pack(side = 'top',fill ='x')
  54. e = Frame(win)
  55. txtlast = Entry(e, width =40)
  56. lname = Label(e, width = 15,text = 'Last Name')
  57. lname.pack(side = 'left',pady = 8)
  58. txtlast.pack(side = 'left')
  59. e.pack(side = 'top',fill ='x')
  60. f = Frame(win)
  61. txtadd = Entry(f, width =40)
  62. address = Label(f, width = 15,text = 'Street Address')
  63. address.pack(side = 'left',pady = 8)
  64. txtadd.pack(side = 'left')
  65. f.pack(side = 'top',fill ='x')
  66. g = Frame(win)
  67. txtcity = Entry(g, width =16)
  68. city = Label(g, width = 15,text = 'City')
  69. city.pack(side = 'left',pady = 8)
  70. txtcity.pack(side = 'left')
  71. txtst = Entry(g, width =4)
  72. street = Label(g, width = 8,text = 'State')
  73. street.pack(side = 'left',pady = 8)
  74. txtst.pack(side = 'left')
  75. txtzip = Entry(g, width =8)
  76. zipcde = Label(g, width = 8,text = 'Zipcde')
  77. zipcde.pack(side = 'left',pady = 8)
  78. txtzip.pack(side = 'left')
  79. g.pack(side = 'top',fill ='x')
  80. h = Frame(win)
  81. txtph = Entry(h, width =16)
  82. phone = Label(h, width = 15,text = 'Phone')
  83. phone.pack(side = 'left',pady = 5)
  84. txtph.pack(side = 'left')
  85. txtpx = Entry(h, width =16)
  86. email = Label(h, width = 10,text = 'E-mail')
  87. email.pack(side = 'left',pady = 8)
  88. txtpx.pack(side = 'left')
  89. h.pack(side = 'top',fill ='x')
  90. j=Frame(root,bd=3,bg='darkblue')
  91. j.pack(side='left',fill='y')
  92. save = Button(j, text = "SAVE",command=project.savefile)
  93. save.pack(pady=20, padx =10)
  94. i = Frame(win)
  95. i.pack(side = 'top',fill ='x')
  96. close = Button(i, text = "EXIT",command=project.closefile)
  97. close.pack(side = 'right',pady = 10, padx = 75)
  98.  
  99. GUI()

project.py
Python Syntax (Toggle Plain Text)
  1. # This file is named project.py
  2. filename="addressfile.txt"
  3. filehelpname="readme.txt"
  4.  
  5. def openwindow(self):
  6. AddressBookGui.dialog()
  7.  
  8. def closefile(self):
  9. root.destroy()
  10. def helpfile(self):
  11. textfile = open(filehelpname,'r')
  12. text=textfile.read()
  13. textfile.close()
  14. print text
  15.  
  16. def findname(self):
  17. thelist1.delete(0,END)
  18. thename = findit.get()
  19. x = open(filename,"r")
  20. for y in x:
  21. q = y.split(",")
  22. if thename == q[1]:
  23. thelist1.insert(END,y)
  24. x.close()
  25. def printfile(self):
  26. thelist1.delete(0,END)
  27. x = open(filename,'r')
  28. for y in x:
  29. thelist1.insert(END,y.strip())
  30. x.close()
  31. def deletefile(self):
  32. thelist1.delete(0,END)
  33. thename2 = deleteit.get()
  34. g = open(filename,"r")
  35. for t in g:
  36. f = t.split(",")
  37. if thename2 == f[1]:
  38. del(thelist1[f])
  39.  
  40. def showfile(self):
  41. person = thelist.get(ANCHOR)
  42. x = person.strip().split(',')
  43. def readfile(self):
  44. thelist1.delete(0,END)
  45. x = open(filename,"r")
  46. for y in x:
  47. thelist1.insert(END,y.strip())
  48. x.close()

Any ideas would be greatly appreciated. I have been working on this for days and am ready to cry .

thanks!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dwrick is offline Offline
1 posts
since Apr 2007
May 2nd, 2007
0

Re: Tkinter Addressbook

My suggestion is to break this monster down to smaller parts and test each part before you assemble it all. Get familiar with GUI programming, classes, callbacks, functions and so on.

At first glance there are a lot of mistakes. For instance this line:
printrec= Button(a, text ="PRINT LIST",command=project.printfile(thelist1))
has 2 errors, thelist1 has not been established yet, and command does not accept a function call but a function object.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: python Exercise
Next Thread in Python Forum Timeline: Success! But roadblock...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC