Addressbook in Gtk and python

Hi to all..I did the addressbook in python-gtk..Database as MySql..here is the code in python.
#---------------------Importing the Modules------------------------
import pygtk
import gtk
pygtk.require("2.0")
#--------------Import the mysqldb to interface with mysql and python---------------------------
import MySQLdb
conn=MySQLdb.connect(host="localhost",user="root",passwd="password",db="balaji")
cursor=conn.cursor()
cursor.execute("create table if not exists users (user varchar(20),address varchar(50),email varchar(30),mobile varchar(12))")
#---------------Get All the Widgets in Main class --------------------------
class Address:
def __init__(self):
self.glade="Addressbook.glade"
self.builder=gtk.Builder()
self.builder.add_from_file(self.glade)
self.window1=self.builder.get_object("window1")
self.window2=self.builder.get_object("window2")
self.window3=self.builder.get_object("window3")
self.label=self.builder.get_object("label")
self.user=self.builder.get_object("entry1")
self.address=self.builder.get_object("entry2")
self.email=self.builder.get_object("entry3")
self.mobile=self.builder.get_object("entry4")
self.name=self.builder.get_object("entry5")
self.msg=self.builder.get_object("messagedialog1")
self.tree=self.builder.get_object("treeview1")
self.list=self.builder.get_object("liststore1")
self.x=0
self.clear()
dic={"on_window1_destroy":self.exit,"on_Add_clicked":self.add,"on_Ad_clicked":self.create,"on_Cancel_clicked":self.quit,
"on_search_clicked":self.search,"on_quit_clicked":self.quit,"on_submit_clicked":self.criteria,
"on_exit_clicked":self.exit,"on_okbutton_clicked":self.okay,"on_cancel_clicked":self.cancell

}
self.builder.connect_signals(dic)
self.window1.show()
#---------------Function to exit the code----------------------
def exit(self, widget, data=None):
gtk.main_quit()
def add(self,widget,data=None):
self.window1.hide()
self.window2.show()
#--------------Function to Add new User into Database--------------------
def create(self,widget,data=None):
user=self.user.get_text()
address=self.address.get_text()
email=self.email.get_text()
mobile=self.mobile.get_text()
a=len(user)
b=len(address)
c=len(email)
d=len(mobile)
if(a==0 or b==0 or c==0 or d==0):
self.x=0
self.msg.set_markup("Please enter all the fields")
self.msg.show()

else:
cursor.execute("""insert into users (user,address,email,mobile) values (%s,%s,%s,%s)""",(user,address,email,mobile))
self.x=1
self.msg.set_markup("Entered successfully")
cursor.execute("""select * from users""")
result=()
result=cursor.fetchall()
print type(result)
self.list.clear()
for row in result:
self.list.append([row[0],row[1],row[2],row[3]])
self.msg.show()
#---------------------Function for Search operation------------------
def search(self,widget,data=None):
self.window3.show()
def okay(self,wideget,data=None):
self.msg.hide()
if(self.x==1):
self.clear()
self.window1.show()
else:
self.window2.show()
def cancell(self,wideget,data=None):
self.msg.hide()
if(self.x==1):
self.clear()
self.window1.show()
else:
self.window2.show()
def clear(self):
self.user.set_text("")
self.address.set_text("")
self.email.set_text("")
self.mobile.set_text("")
def criteria(self,widget,data=None):
name=self.name.get_text()
conn=MySQLdb.connect(host="localhost",user="root",passwd="password",db="balaji")
cursor=conn.cursor()
cursor.execute("""select * from users where user=(%s)""",(name))
result=()
result=cursor.fetchall()
self.list.clear()
for row in result:
self.list.append([row[0],row[1],row[2],row[3]])
def quit(self,widget,data=None):
self.window1.show()
self.window2.hide()
self.window3.hide()
def exit(self,widget,data=None):
gtk.main_quit()
if __name__ == "__main__":
addr= Address()
gtk.main()

Advertisement

AddressBook in python

Hi to all…here is the code for address book in python


#!usr/bin/python

import sys
import MySQLdb
conn=MySQLdb.connect(host="localhost",user="root",passwd="password",db="balaji")
cursor=conn.cursor()
cursor.execute("create table if not exists users (name varchar(20),address varchar(50),email varchar(30),mobile varchar(12))")

def printt():
ch=int (raw_input("1.Add New 2.Search 3.Exit \n Enter Your Choice"))
while(ch!=4):

if(ch==1):
name=raw_input("Enter a Name")
address=raw_input("Enter address")
email=raw_input("enter Email address")
mobile=raw_input("enter mobile no.")
cursor.execute("""insert into users (name,address,email,mobile) values (%s,%s,%s,%s)""",(name,address,email,mobile))
option=raw_input("Add Another Record y/n")
if(option=='y'):
ch=1
else:
printt()

if(ch==2):
name1=raw_input("Enter a name to Search")
cursor.execute("""select * from users where name=(%s)""",(name1))
result=cursor.fetchall()
for row in result:
name=row[0]
address=row[1]
email=row[2]
mobile=row[3]
print " name=%s \n address=%s \n email=%s \n mobile=%s" % (name,address,email,mobile)
option=raw_input("Search Another Name (y/n)")
if(option=='y'):
ch=2
else:
printt()
if(ch==3):
sys.exit()

printt()