0

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

# This file is named AddressBookGui.py
from Tkinter import *
import tkMessageBox
from ScrolledText import *
import project

class GUI:
  def __init__(self):
    root = Tk()
    root.title("Simple Address Book")
    root.geometry('500x400')
    b =Frame(root, bd = 3, bg='darkblue')
    b.pack(side='top', fill = 'x')
    findit = Entry(b, width=30)
    find = Button(b, text = "SEARCH LASTNAME",command=project.findname)
    find.pack(side = "right", pady = 5, padx = 10)
    findit.pack(side='right')
    record = Button(b, text ="NEW RECORD",command=project.openwindow)
    record.pack(side = "left", pady = 10, padx = 10)
    a = Frame(root, bd = 3,bg='darkblue')
    a.pack(side='left', fill = 'y')
    openfile = Button(a, text = "OPEN",command=project.readfile)
    openfile.pack(side = "top", pady =10, padx =10)
    printrec= Button(a, text ="PRINT LIST",command=project.printfile(thelist1))
    printrec.pack(pady = 40, padx = 15)
    helpfile = Button(a, text = "HELP",command=project.helpfile)
    helpfile.pack(side = "bottom", pady =20, padx =10)
    close = Button(a, text = "EXIT",command=project.closefile)
    close.pack(side = 'bottom',pady = 10, padx = 10)
    c=Frame(root,bd=3,bg='darkblue')
    c.pack(side='bottom',fill='x')
    deleteit = Entry(c, width=30)
    delete = Button(c, text = "DELETE NAME",command=project.deletefile)
    delete.pack(side="right",pady = 25, padx = 10)
    deleteit.pack(side='right')
    c = Frame(root, bd = 3, relief = 'groove')
    c.pack(side='top', fill = 'x')
    i =Frame(root)
    thelist1=Listbox(i,bg = 'white')
    thelist1.pack(fill = 'both')
    i.pack(side = 'top',fill = 'both',expand = 1)
    thetext = ScrolledText(i)
    thetext.pack(side = 'top',fill = 'both',expand =1)
    root.mainloop()
def dialog(frame):
    win = Toplevel()
    win.geometry('400x230+100+100')
    d = Frame(win)
    txtfirst = Entry(d, width =40)
    fname = Label(d, width = 15,text = 'First Name')
    fname.pack(side = 'left',pady = 8)
    txtfirst.pack(side = 'left')
    d.pack(side = 'top',fill ='x')
    e = Frame(win)
    txtlast = Entry(e, width =40)
    lname = Label(e, width = 15,text = 'Last Name')
    lname.pack(side = 'left',pady = 8)
    txtlast.pack(side = 'left')
    e.pack(side = 'top',fill ='x')
    f = Frame(win)
    txtadd = Entry(f, width =40)
    address = Label(f, width = 15,text = 'Street Address')
    address.pack(side = 'left',pady = 8)
    txtadd.pack(side = 'left')
    f.pack(side = 'top',fill ='x')
    g = Frame(win)
    txtcity = Entry(g, width =16)
    city = Label(g, width = 15,text = 'City')
    city.pack(side = 'left',pady = 8)
    txtcity.pack(side = 'left')
    txtst = Entry(g, width =4)
    street = Label(g, width = 8,text = 'State')
    street.pack(side = 'left',pady = 8)
    txtst.pack(side = 'left')
    txtzip = Entry(g, width =8)
    zipcde = Label(g, width = 8,text = 'Zipcde')
    zipcde.pack(side = 'left',pady = 8)
    txtzip.pack(side = 'left')
    g.pack(side = 'top',fill ='x')
    h = Frame(win)
    txtph  = Entry(h, width =16)
    phone = Label(h, width = 15,text = 'Phone')
    phone.pack(side = 'left',pady = 5)
    txtph.pack(side = 'left')
    txtpx  = Entry(h, width =16)
    email = Label(h, width = 10,text = 'E-mail')
    email.pack(side = 'left',pady = 8)
    txtpx.pack(side = 'left')
    h.pack(side = 'top',fill ='x')
    j=Frame(root,bd=3,bg='darkblue')
    j.pack(side='left',fill='y')
    save = Button(j, text = "SAVE",command=project.savefile)
    save.pack(pady=20, padx =10)    
    i = Frame(win)
    i.pack(side = 'top',fill ='x')
    close = Button(i, text = "EXIT",command=project.closefile)
    close.pack(side = 'right',pady = 10, padx = 75)

GUI()

project.py

# This file is named project.py
filename="addressfile.txt"
filehelpname="readme.txt"

def openwindow(self):
    AddressBookGui.dialog()
    
def closefile(self):
    root.destroy()
def helpfile(self):
    textfile = open(filehelpname,'r')
    text=textfile.read()
    textfile.close() 
    print text
   
def findname(self):
    thelist1.delete(0,END)
    thename = findit.get()
    x = open(filename,"r")
    for y in x:
        q = y.split(",")
        if thename == q[1]:
            thelist1.insert(END,y)
    x.close()
def printfile(self):
    thelist1.delete(0,END)
    x = open(filename,'r')
    for y in x:
        thelist1.insert(END,y.strip())
    x.close()
def deletefile(self):
    thelist1.delete(0,END)
    thename2 = deleteit.get()
    g = open(filename,"r")
    for t in g:
        f = t.split(",")
        if thename2 == f[1]:
            del(thelist1[f])
    
def showfile(self):
    person = thelist.get(ANCHOR)
    x = person.strip().split(',')
def readfile(self):
    thelist1.delete(0,END)
    x = open(filename,"r")
    for y in x:
        thelist1.insert(END,y.strip())
    x.close()

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

thanks!

2
Contributors
1
Reply
2
Views
10 Years
Discussion Span
Last Post by vegaseat
0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.