Hello. I was actually trying to build a sms – cum – database api using way2sms but I got stuck at this point.
So I thought I would finish the database part of my app first using gtk and shelve.
class Window: def __init__ (self): self.window = Gtk.Window(title = 'Store Contacts') self.window.connect('delete-event',Gtk.main_quit) self.window.set_size_request(300,300) self.table = Gtk.Table(10,10,True) self.entry1 = Gtk.Entry() self.entry1.set_text('Enter name') self.entry1.connect('changed',self.search) self.entry2 = Gtk.Entry() self.entry2.set_text('Enter phone number') self.button3 = Gtk.Button(label = 'Store Contact') self.button3.connect('clicked',self.store) self.button4 = Gtk.Button(label = 'Search Contact') self.table.attach(self.entry1,2,8,2,3) self.table.attach(self.entry2,2,8,4,5) self.table.attach(self.button3,2,4,6,8) self.table.attach(self.button4,6,8,6,8) self.window.add(self.table) self.window.show_all()
As simple as that , my main window contains two buttons , two entry boxes , the default entry which is Enter
name and Enter phone number.The entry1 is connected to a function search by an event clicked and the button3
is connected to a function store which I would explain later.This is how my main window looks like
Now for the two functions.
This is my function connected to button3
def store(self,widget): name = self.entry1.get_text() name = ''.join(list(name.strip().lower())) phone_no = self.entry2.get_text() contacts = shelve.open('database.dat') for contact in contacts: if contact == name: self.Dialog(contact,phone_no) return contacts[name] = phone_no contacts.close()
As you can see once the button is clicked , it retrieves the name and phone number from the two entry boxes.
The shelve module is great because it opens a dictionary and can be used to store stuff that cannot be
manipulated unless you delete the file.Once a name is entered it checks for all contacts and if not present
it stores in the dictionary.If present it opens another window confirming whether the given contact should be
replaced or not which looks like this.
The fuction for the dialog window is
def Dialog(self,contact,phone_no): window = Gtk.Window() window.set_size_request(100,100) button1 = Gtk.Button(label = 'YES') label = Gtk.Label('%s already exists.Do you want to replace' % (contact)) button2 = Gtk.Button(label = 'NO') tabel = Gtk.Table(5,5,True) tabel.attach(label,1,4,1,2) tabel.attach(button1,1,2,3,4) tabel.attach(button2,3,4,3,4) window.add(tabel) button1.connect('clicked',self.check,1,window,contact,phone_no) button2.connect('clicked',self.check,2,window,contact,phone_no) window.show_all()
It creates a window with two buttons.If clicked YES , contact is replaced.
and then the final function search which automatically searches if the given contact is present or not.
def search(self,widget): text = (widget.get_text()).lower() contacts = shelve.open('database.dat') for contact in contacts: if contact == text: self.entry2.set_text(contacts[contact]) contacts.close() return self.entry2.set_text('No search found') contacts.close() return
Thus you have a simple contacts app which can be used to store numbers.
The following things should be noted.
if __name__ == '__main__' :
is used when you want the program to
run only from the terminal.That means you can input the script as a module,the script does not run
and do script.fun to use the
2.putting self. before your variable means that your variables become attributes of the class object and are
automatically passed through when you put self.Notice I did not put self. in the Dialog function because that
is the new window is not an attribute of the older window.
Thanks for reading.