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()

Author: Balaji

Hi..My name is Balaji and i am working as a Senior Software Developer in India. I am interested in Shell scripts, python, erlang , linux kernel .

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s