Hi! Hope you're enjoying this blog. I have a new home at www.goldsborough.me. Be sure to also check by there for new posts <3

Thursday, September 5, 2013

Fully functional PyQt address book

Hi, just wanted to post the code for a program I did a little while ago. It's an address book that let's you store some data about a person. The buttons are in tile-style and a button get's added dynamically when you create a new contact. Have fun.

import sys,pickle,time
from PyQt4 import QtGui, QtCore


f = open("contacts.txt","rb")
try:
    contacts = pickle.loads(f.read())
    print("1")
except:
    print("2")
    contacts = {}
finally:
    f.close()

class New(QtGui.QDialog):

    def __init__(self,parent=None):
        global nvar,cvar,wvar,ovar
        
        QtGui.QDialog.__init__(self,parent)

        self.initUI()

        nvar = False
        cvar = False
        wvar = False
        ovar = False

    def initUI(self):

        self.name = QtGui.QPushButton("Name",self)
        self.name.clicked.connect(self.Name)

        self.fname = QtGui.QLabel("First name",self)
        self.fnameline = QtGui.QLineEdit(self)

        self.lname = QtGui.QLabel("Last name",self)
        self.lnameline = QtGui.QLineEdit(self)

        self.contact = QtGui.QPushButton("Contact",self)
        self.contact.clicked.connect(self.Contact)

        self.num1 = QtGui.QLabel("Telephone number 1",self)
        self.numline1 = QtGui.QLineEdit(self)

        self.numtype1 = QtGui.QComboBox(self)

        self.numtype1.addItem("Mobile")
        self.numtype1.addItem("Work")
        self.numtype1.addItem("Home")
        self.numtype1.addItem("Fax")

        self.num2 = QtGui.QLabel("Telephone number 2",self)
        self.numline2 = QtGui.QLineEdit(self)

        self.numtype2 = QtGui.QComboBox(self)

        self.numtype2.addItem("Mobile")
        self.numtype2.addItem("Work")
        self.numtype2.addItem("Home")
        self.numtype2.addItem("Fax")

        self.email = QtGui.QLabel("Email",self)
        self.emailline = QtGui.QLineEdit(self)

        self.work = QtGui.QPushButton("Work",self)
        self.work.clicked.connect(self.Work)

        self.title = QtGui.QLabel("Title",self)
        self.titleline = QtGui.QLineEdit(self)

        self.company = QtGui.QLabel("Company",self)
        self.companyline = QtGui.QLineEdit(self)

        self.position = QtGui.QLabel("Position",self)
        self.positionline = QtGui.QLineEdit(self)

        self.compsite = QtGui.QLabel("Company Website",self)
        self.compsiteline = QtGui.QLineEdit(self)

        self.other = QtGui.QPushButton("Other",self)
        self.other.clicked.connect(self.Other)

        self.address = QtGui.QLabel("Address",self)
        self.addressline = QtGui.QLineEdit(self)

        self.website = QtGui.QLabel("Website",self)
        self.websiteline = QtGui.QLineEdit(self)

        self.birthday = QtGui.QLabel("Birthday",self)
        self.birthdayline = QtGui.QDateEdit(self)

        self.notes = QtGui.QLabel("Notes",self)
        self.notesline = QtGui.QTextEdit(self)

        self.save = QtGui.QPushButton("Save",self)
        self.save.clicked.connect(self.Save)
        
        self.cancel = QtGui.QPushButton("Cancel",self)
        self.cancel.clicked.connect(lambda: self.hide())

        sub = [self.fname,self.fnameline,self.lname,self.lnameline,
               self.num1,self.numline1,self.numtype1,self.num2,
               self.numline2,self.numtype2,self.email,self.emailline,
               self.title,self.titleline,self.company,
               self.companyline,self.position,self.positionline,self.compsite,
               self.compsiteline,self.address,self.addressline,self.website,
               self.websiteline,self.birthday,self.birthdayline,self.notes,
               self.notesline]

        main = [self.name,self.contact,self.work,self.other]

        widgets = [self.name,self.fname,self.fnameline,self.lname,self.lnameline,
                 self.contact,self.num1,self.numline1,self.numtype1,self.num2,
                 self.numline2,self.numtype2,self.email,self.emailline,
                 self.work,self.title,self.titleline,self.company,
                 self.companyline,self.position,self.positionline,self.compsite,
                 self.compsiteline,self.other,self.address,self.addressline,self.website,
               self.websiteline,self.birthday,self.birthdayline,self.notes,
               self.notesline]

        for i in sub:
            i.hide()

        grid = QtGui.QGridLayout(self)

        pos = 0

        for i in widgets:
            grid.addWidget(i,pos,0,1,2)
            pos +=1

        grid.addWidget(self.save,pos,0,1,1)
        grid.addWidget(self.cancel,pos,1,1,1)

        self.setLayout(grid)

        self.setGeometry(300,200,175,100)
        self.setWindowTitle("Add contact")
        self.setStyleSheet("font-size:13px")

    def Name(self):
        global nvar
        
        if nvar == False:
            self.fname.show()
            self.fnameline.show()
            self.lname.show()
            self.lnameline.show()

            nvar = True
            
        else:
            self.fname.hide()
            self.fnameline.hide()
            self.lname.hide()
            self.lnameline.hide()

            nvar = False

        self.resize(175,100)

    def Contact(self):
        global cvar
        
        if cvar == False:
            self.num1.show()
            self.numline1.show()
            self.numtype1.show()
            self.num2.show()
            self.numline2.show()
            self.numtype2.show()
            self.email.show()
            self.emailline.show()

            cvar = True
            
        else:
            self.num1.hide()
            self.numline1.hide()
            self.numtype1.hide()
            self.num2.hide()
            self.numline2.hide()
            self.numtype2.hide()
            self.email.hide()
            self.emailline.hide()

            cvar = False

        self.resize(175,100)

    def Work(self):
        global wvar
        
        if wvar == False:
            self.title.show()
            self.titleline.show()
            self.company.show()
            self.companyline.show()
            self.position.show()
            self.positionline.show()
            self.compsite.show()
            self.compsiteline.show()

            wvar = True
            
        else:
            self.title.hide()
            self.titleline.hide()
            self.company.hide()
            self.companyline.hide()
            self.position.hide()
            self.positionline.hide()
            self.compsite.hide()
            self.compsiteline.hide()

            wvar = False

        self.resize(175,100)

    def Other(self):
        global ovar

        if ovar == False:
            self.address.show()
            self.addressline.show()
            self.website.show()
            self.websiteline.show()
            self.birthday.show()
            self.birthdayline.show()
            self.notes.show()
            self.notesline.show()

            ovar = True

        else:
            self.address.hide()
            self.addressline.hide()
            self.website.hide()
            self.websiteline.hide()
            self.birthday.hide()
            self.birthdayline.hide()
            self.notes.hide()
            self.notesline.hide()

            ovar = False

        self.resize(175,100)


    def Save(self):
        global contacts,button

        name = self.fnameline.text() + " " + self.lnameline.text()
        fname = self.fnameline.text()
        lname = self.lnameline.text()
        num1 = self.numline1.text()
        numtype1 = self.numtype1.currentIndex()
        num2 = self.numline2.text()
        numtype2 = self.numtype2.currentIndex()
        email = self.emailline.text()
        title = self.titleline.text()
        company = self.companyline.text()
        position = self.positionline.text()
        compsite = self.compsiteline.text()
        address = self.addressline.text()
        birthday = self.birthdayline.date()
        notes = self.notesline.toPlainText()

        if button == "+":
            contacts[name] = {"First name":fname,
                              "Last name":lname,
                              "Telephone number 1":num1,
                              "Type 1":numtype1,
                              "Telephone number 2":num2,
                              "Type 2":numtype2,
                              "Email":email,
                              "Title":title,
                              "Company":company,
                              "Position":position,
                              "Company Website":compsite,
                              "Address":address,
                              "Birthday":birthday,
                              "Notes":notes
                              }
        else:
            del contacts[button]
            contacts[name] = {"First name":fname,
                              "Last name":lname,
                              "Telephone number 1":num1,
                              "Type 1":numtype1,
                              "Telephone number 2":num2,
                              "Type 2":numtype2,
                              "Email":email,
                              "Title":title,
                              "Company":company,
                              "Position":position,
                              "Company Website":compsite,
                              "Address":address,
                              "Birthday":birthday,
                              "Notes":notes
                              }

        f = open("contacts.txt","wb")
        pickle.dump(contacts,f)
        f.close()
        
        self.close()
        
class Main(QtGui.QMainWindow):

    def __init__(self,parent=None):
        QtGui.QMainWindow.__init__(self,parent)
        self.initUI()

    def initUI(self):
        global contacts
        
        centralwidget = QtGui.QWidget()

        self.timer = QtCore.QTimer(self)
        self.timer.start(10)
        self.timer.timeout.connect(self.Hover)

        self.add = QtGui.QPushButton("+",self)
        self.add.setStyleSheet("font-size:40px;background-color:#333333;border: 2px solid #222222")
        self.add.setFixedSize(100,100)

        self.add.clicked.connect(self.Add)

        self.grid = QtGui.QGridLayout()

        self.grid.addWidget(self.add,0,0)
        
        centralwidget.setLayout(self.grid)

        if contacts:
            self.addTile()

        self.setCentralWidget(centralwidget)
     
#---------Window settings --------------------------------
        
        self.setGeometry(300,300,500,100)
        self.setWindowTitle("PyTact")

    def clickContact(self):
        global contacts, button

        sender = self.sender()
        
        ind = self.sender().text().index("\n")
        button = self.sender().text()[:ind] + self.sender().text()[ind+1:]
        
        contact_id = contacts[button]

        self.timer.start(150)
        sender.setStyleSheet("font-size:15px;background-color:#666666;border: 2px solid #555555")

        new = New(self)

        fname = contact_id["First name"]
        new.fnameline.setText(fname)
        lname = contact_id["Last name"]
        new.lnameline.setText(lname)
        num1 = contact_id["Telephone number 1"]
        new.numline1.setText(num1)
        numtype1 = contact_id["Type 1"]
        new.numtype1.setCurrentIndex(numtype1)
        num2 = contact_id["Telephone number 2"]
        new.numline2.setText(num2)
        numtype2 = contact_id["Type 2"]
        new.numtype2.setCurrentIndex(numtype2)
        email = contact_id["Email"]
        new.emailline.setText(email)
        title = contact_id["Title"]
        new.titleline.setText(title)
        company = contact_id["Company"]
        new.companyline.setText(company)
        position = contact_id["Position"]
        new.positionline.setText(position)
        compsite = contact_id["Company Website"]
        new.compsiteline.setText(compsite)
        address = contact_id["Address"]
        new.addressline.setText(address)
        birthday = contact_id["Birthday"]
        new.birthdayline.setDate(birthday)
        notes = contact_id["Notes"]
        new.notesline.setText(notes)

        new.show()

        new.save.clicked.connect(self.addTile)

    def ContextMenu(self):
        global sender
        sender = self.sender()
        
        self.menu = QtGui.QMenu(self)

        remove = QtGui.QAction("Remove",self)
        remove.triggered.connect(self.Remove)

        self.menu.addAction(remove)

        self.menu.show()

    def Remove(self):
        global sender
        global contacts

        del contacts[sender.text()]

        sender.setParent(None)
        
    def addTile(self):
        global contacts
        
        for i in reversed(range(self.grid.count())):
            self.grid.itemAt(i).widget().setParent(None)

        self.grid.addWidget(self.add,0,0)

        h = 1
        v = 0

        for i in contacts.keys():

            ind = i.rindex(" ")
            t = i[:ind]+ "\n" + i[ind:]
            
            b = QtGui.QPushButton(t,self)
            b.setStyleSheet("color:#0191C8;font-size:15px;background-color:#333333;border: 2px solid #222222")
            b.setFixedSize(100,100)
            b.clicked.connect(self.clickContact)

            b.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
            b.customContextMenuRequested.connect(self.ContextMenu)

            self.grid.addWidget(b,v,h)

            h += 1

            if h > 3 and h % 4 == 0:
                v += 1
                h = 0

    def Add(self):
        global button
        
        self.timer.start(150)
        self.add.setStyleSheet("color:#0191C8;font-size:40px;background-color:#666666;border: 2px solid #555555")

        button = self.sender().text()
        
        new = New(self)
        new.show()

        new.save.clicked.connect(self.addTile)

    def Hover(self):
        self.timer.start(10)
        for i in reversed(range(self.grid.count())):
            if i > 0:
                if self.grid.itemAt(i).widget().underMouse() == True:
                    self.grid.itemAt(i).widget().setStyleSheet("color:#0191C8;font-size:15px;background-color:#444444;border: 2px solid #333333")
                else:
                    self.grid.itemAt(i).widget().setStyleSheet("color:#0191C8;font-size:15px;background-color:#333333;border: 2px solid #222222")
            else:
                if self.add.underMouse() == True:
                    self.add.setStyleSheet("color:#0191C8;font-size:40px;background-color:#444444;border: 2px solid #333333")
                else:
                    self.add.setStyleSheet("color:#0191C8;font-size:40px;background-color:#333333;border: 2px solid #222222")


def main():
    app = QtGui.QApplication(sys.argv)
    main= Main()
    main.show()

    sys.exit(app.exec_())

if __name__ == "__main__":
    main()

No comments :

Post a Comment