#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    3
    Rep Power
    0

    Moving code to workable GUI


    Hi, im new to python and these forums as well. I have some working code for a contact manger program. Basically I would like to add it to my GUI that I created, so for example instead of the program asking me in the shell to type in the contact info I would type it into the fields in the GUI and hit the add contact button and it would add it to the contact manager. I just need a step in the right direction. Any help at all would be great, ill post my code below. Thanks in advance.

    ######Working Contact Manager#############

    import sys

    class Person:
    first_name = ""
    last_name = ""
    phone_number = ""

    def __init__(self, fName, lName, phone):
    self.first_name = fName
    self.last_name = lName
    self.phone_number = phone

    def get_info(self):
    print(self.first_name, self.last_name, self.phone_number)

    class Friend(Person):
    email = ""
    birth_date = ""

    def get_info(self):
    print(self.email, self.birth_date)

    def getContactInfo():
    f = input("Enter First Name")
    l = input("Enter Last Name")
    p = input("Enter Phone Number")
    a = Person(f, l, p)
    return a

    def getFriendInfo():
    f = input("Enter First Name")
    l = input("Enter Last Name")
    p = input("Enter Phone Number")
    e = input("Enter Email")
    b = input("Enter Birth Date")
    a = Friend(f, l, p)
    a.email = e
    a.birth_date = b
    return a

    def lookUpContact():
    global contactList
    l = str(input("Please enter the last name of the contact: "))
    for item in contactList:
    if l == item.last_name:
    print_info(item)

    def print_friend_info(contact):
    print(contact.first_name)
    print(contact.last_name)
    print(contact.phone_number)
    print(contact.email)
    print(contact.birth_date)

    def print_info(contact):
    print(contact.first_name)
    print(contact.last_name)
    print(contact.phone_number)

    def mainMenu():

    global contactList

    choice = input("Enter 1 to add a contact" "Enter 2 to search for a contact" "Enter 3 to exit")

    if choice == "1":
    type_of_contact = input("Do you want to add a 'friend' or a 'person'")
    if type_of_contact == "person":

    contact = getContactInfo()
    contactList.append(contact)

    print_info(contact)
    mainMenu()


    else:

    contact = getFriendInfo()
    contactList.append(contact)

    print_friend_info(contact)
    mainMenu()

    elif choice == "2":
    lookUpContact()
    mainMenu()

    elif choice == "3":
    print("Goodbye")
    sys.exit(1)

    else:
    print("Please enter a valid menu option.")
    mainMenu()


    def main():
    while True:
    mainMenu()

    contactList = list()
    main()



    ###########My GUI code (I added the classes from the previous code already)###########


    from tkinter import*

    #################################################################################################### #####
    # Create my Classes

    class Person:
    first_name = ""
    last_name = ""
    phone_number = ""

    def __init__(self, fName, lName, phone):
    self.first_name = fName
    self.last_name = lName
    self.phone_number = phone

    def get_info(self):
    print(self.first_name, self.last_name, self.phone_number)

    class Friend(Person):
    email = ""
    birth_date = ""

    def get_info(self):
    print(self.email, self.birth_date)




    #################################################################################################### #####
    #Create my Window

    app = Tk()
    app.title("Contact Manager")
    app.geometry('400x300')


    #################################################################################################### ###########
    # Functions

    def add_contact_button():
    pass

    def search():
    pass

    def display():
    pass

    #################################################################################################### ######
    # Create Buttons, List box, And all labels

    cl = Label(app, text = "Contact List")
    cl.pack()
    cl.place(x = 40, y = 0)

    nc = Label(app, text = "New Contact")
    nc.pack()
    nc.place(x = 200, y = 0)

    fn = Label(app, text = "First Name:")
    fn.pack()
    fn.place(x = 140, y = 20)
    firstname = Entry(app)
    firstname.pack()
    firstname.place(x = 210, y = 20)


    ln = Label(app, text = "Last Name:")
    ln.pack()
    ln.place(x = 140, y = 40)
    lastname = Entry(app)
    lastname.pack()
    lastname.place(x = 210, y = 40)

    pn = Label(app, text = "Phone #:")
    pn.pack()
    pn.place(x = 140, y = 60)
    phone = Entry(app)
    phone.pack()
    phone.place(x = 210, y = 60)

    em = Label(app, text = "Email:")
    em.pack()
    em.place(x = 140, y = 100)
    email = Entry(app)
    email.pack()
    email.place(x = 210, y = 110)

    bd = Label(app, text = "Birthday:")
    bd.pack()
    bd.place(x = 140, y = 120)
    birthday = Entry(app)
    birthday.pack()
    birthday.place(x = 210, y = 130)

    sln = Label(app, text = "Last Name:")
    sln.pack()
    sln.place(x = 20, y = 240)
    searchlastname = Entry(app)
    searchlastname.pack()
    searchlastname.place(x = 85, y = 240)

    listbox = Listbox(app)
    listbox.pack()
    listbox.place(x = 10, y = 40)

    c1 = Checkbutton(app)
    c1.pack()
    c1.place(x = 210, y = 80)
    checkbox = Label(app, text = "Friend")
    checkbox.pack()
    checkbox.place(x = 230, y = 80)

    b1 = Button(app, text = "Add Contact", command = add_contact_button)
    b1.pack()
    b1.place(x = 260, y = 160)

    b2 = Button(app, text = "Search", command = search)
    b2.pack()
    b2.place(x = 20, y = 270)

    b3 = Button(app, text = "Display Contact", width = 13)
    b3.pack()
    b3.place(x = 30 , y = 205)

    #################################################################################################### ######
    # Loop

    app.mainloop()
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    43
    Rep Power
    3
    You need to add code tags around your code to preserve indentation. Since Python uses tabs as syntax, your code is useless unless the tabs are preserved. Anyone interested in helping you would have to guess where you've indented your syntax and spend 20 minutes fixing your code just because you posted it wrong.

    So you probably won't get much help until you fix that. Just wrap your Python code in BBCode Code tags

    PHP Code:
    [code][/code
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    3
    Rep Power
    0
    Oops. Thanks for the tip. How do I add code tags?? I've never posted code online before
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    3
    Rep Power
    0
    Oops. Thanks for the tip. How do I add code tags?? I've never posted code online before
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481
    Text version:

    Your program doesn't work. You merely haven't tested it sufficiently. The big problem is that it doesn't save data between uses. The bigger problem is that it crashes with lots of use.

    $ yes 2 | ( cd /tmp && python3 p.py )
    (100 thousand characters omitted)
    Enter 1 to add a contactEnter 2 to search for a contactEnter 3 to exitFatal Python error: Cannot recover from stack overflow.
    yes: standard output: Broken pipe
    yes: write error

    Easily repaired. Remove all the recursive mainMenu calls from within mainMenu




    GUI version. You're using both the pack and place managers. The place manager takes precedence. Maybe because of the order. Whatever. Remove every line in the code that says .pack() .

    In my mind, search and display provide the same functionality. I removed one of them.

    I've include __str__ and __eq__ methods into your Person class, changed the ListBox to Text, and used some variables. This works but I won't call it good code. As others have pointed out, you're better off having a class to contain all the gui glop rather than have such a dazzling array of global variables.

    Oh, also you won't need to modify the labels. I wouldn't bother saving them at the python level. Thus, instead of
    sln = tkinter.Label(app, text = "Last Name:")
    sln.place(x = 20, y = 240)
    I use
    tkinter.Label(app, text = "Last Name:").place(x = 20, y = 240)

    And I didn't fix positioning. Widgets overlap.
    Code:
    import tkinter
    
    contacts = []
    
    #########################################################################################################
    # Create my Classes
    
    class Person:
        first_name = ""
        last_name = ""
        phone_number = ""
    
        def __init__(self, fName, lName, phone):
             self.first_name = fName
             self.last_name = lName
             self.phone_number = phone
    
        def get_info(self):
            print(self.first_name, self.last_name, self.phone_number)
    
        def __str__(self):
            return '{}, {}  {}'.format(self.first_name, self.last_name, self.phone_number)
    
        def __eq__(self, lName, fName = None):
            return (lName == self.last_name) and ((fName is None) or (fName == self.first_name))
    
    class Friend(Person):
        email = ""
        birth_date = ""
    
        def get_info(self):
            print(self.email, self.birth_date)
    
        def __str__(self):
            a = super().__str__()
            b = '{}, {}'.format(self.email, self.birth_date)
            return a+'\n'+b
    
    #########################################################################################################
    #Create my Window
    
    app = tkinter.Tk()
    app.title("Contact Manager")
    app.geometry('500x500')
    
    ###############################################################################################################
    # Functions
    
    def add_contact_button():
        friend = c1variable.get()
        contact = (Person, Friend,)[friend](firstname.get(),lastname.get(),phone.get())
        if friend:
            contact.email = email.get()
            contact.birth_date = birthday.get()
        contacts.append(contact)
        # extra coding if you want to reset the fields
    
    def display():
        lName = searchlastname.get()
        text.delete('1.0', tkinter.END)
        text.insert(tkinter.END, str(contacts[contacts.index(lName)]) if lName in contacts else 'Unfound')
    
    ##########################################################################################################
    # Create Buttons, List box, And all labels
    
    cl = tkinter.Label(app, text = "Contact List").place(x = 40, y = 0)
    
    nc = tkinter.Label(app, text = "New Contact").place(x = 200, y = 0)
    
    fn = tkinter.Label(app, text = "First Name:").place(x = 140, y = 20)
    #Lambert Electronics, LLC.  USA, NY.
    firstname = tkinter.Entry(app)
    firstname.place(x = 210, y = 20)
    
    
    ln = tkinter.Label(app, text = "Last Name:").place(x = 140, y = 40)
    lastname = tkinter.Entry(app)
    lastname.place(x = 210, y = 40)
    
    pn = tkinter.Label(app, text = "Phone #:").place(x = 140, y = 60)
    phone = tkinter.Entry(app)
    phone.place(x = 210, y = 60)
    
    em = tkinter.Label(app, text = "Email:").place(x = 140, y = 100)
    email = tkinter.Entry(app)
    email.place(x = 210, y = 110)
    
    bd = tkinter.Label(app, text = "Birthday:").place(x = 140, y = 120)
    birthday = tkinter.Entry(app)
    birthday.place(x = 210, y = 130)
    
    tkinter.Label(app, text = "Last Name:").place(x = 20, y = 240)
    searchlastname = tkinter.Entry(app)
    searchlastname.place(x = 85, y = 240)
    
    text = tkinter.Text(app, width = 20, height = 2)
    text.place(x = 10, y = 40)
    
    c1variable = tkinter.IntVar()
    c1 = tkinter.Checkbutton(app,variable=c1variable)
    c1.place(x = 210, y = 80)
    checkbox = tkinter.Label(app, text = "Friend").place(x = 230, y = 80)
    
    b1 = tkinter.Button(app, text = "Add Contact", command = add_contact_button)
    b1.place(x = 260, y = 160)
    
    b3 = tkinter.Button(app, text = "Display Contact", command = display,  width = 13)
    b3.place(x = 30 , y = 205)
    
    ##########################################################################################################
    # Loop
    
    app.mainloop()
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo