Created a Phone directory using Glade with Python


Hi friends ,

I have created a small Application using glade with python .. Glade is ” A USER INTERFACE DESIGNER ” ..

It can be used with many progaming languages like Python , ruby , C++ etc to create Application .. Glade will be saved as XML document ..

and save it by (.glade ) Extension ..

I choose to create this application using python .. Before running the script , Be ready with empty file “contacts.txt” ..

Whrere all your contacts will be taken as back-up ..

First glade file is

<?xml version="1.0"?>
<glade-interface>
  <!-- interface-requires gtk+ 2.16 -->
  <!-- interface-naming-policy project-wide -->
  <widget class="GtkWindow" id="window1">
    <property name="title" translatable="yes">Directory</property>
    <property name="window_position">center</property>
    <property name="default_width">600</property>
    <property name="default_height">200</property>
    <signal name="destroy" handler="on_window1_destroy"/>
    <child>
      <widget class="GtkVBox" id="vbox1">
        <property name="visible">True</property>
        <child>
          <widget class="GtkAccelLabel" id="entry2">
            <property name="visible">True</property>
          </widget>
          <packing>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <widget class="GtkHBox" id="hbox1">
            <property name="visible">True</property>
            <child>
              <widget class="GtkVBox" id="vbox4">
                <property name="visible">True</property>
                <child>
                  <widget class="GtkVBox" id="vbox5">
                    <property name="visible">True</property>
                    <child>
                      <widget class="GtkEntry" id="entry1">
                        <property name="visible">True</property>
                        <property name="can_focus">True</property>
                        <property name="invisible_char">●</property>
                      </widget>
                      <packing>
                        <property name="position">0</property>
                      </packing>
                    </child>
                    <child>
                      <widget class="GtkVBox" id="vbox7">
                        <property name="visible">True</property>
                        <child>
                          <widget class="GtkLabel" id="label1">
                            <property name="visible">True</property>
                            <property name="label" translatable="yes">See the contacts 
by typing above</property>
                          </widget>
                          <packing>
                            <property name="position">0</property>
                          </packing>
                        </child>
                        <child>
                          <placeholder/>
                        </child>
                      </widget>
                      <packing>
                        <property name="position">1</property>
                      </packing>
                    </child>
                  </widget>
                  <packing>
                    <property name="position">0</property>
                  </packing>
                </child>
              </widget>
              <packing>
                <property name="position">0</property>
              </packing>
            </child>
            <child>
              <widget class="GtkVBox" id="vbox6">
                <property name="visible">True</property>
                <child>
                  <widget class="GtkEntry" id="entry3">
                    <property name="visible">True</property>
                    <property name="can_focus">True</property>
                    <property name="invisible_char">●</property>
                  </widget>
                  <packing>
                    <property name="position">0</property>
                  </packing>
                </child>
                <child>
                  <widget class="GtkVBox" id="vbox8">
                    <property name="visible">True</property>
                    <child>
                      <widget class="GtkLabel" id="label2">
                        <property name="visible">True</property>
                        <property name="label" translatable="yes">Save the contacts by 
typiing above</property>
                      </widget>
                      <packing>
                        <property name="position">0</property>
                      </packing>
                    </child>
                    <child>
                      <placeholder/>
                    </child>
                  </widget>
                  <packing>
                    <property name="position">1</property>
                  </packing>
                </child>
              </widget>
              <packing>
                <property name="position">1</property>
              </packing>
            </child>
            <child>
              <widget class="GtkVBox" id="vbox3">
                <property name="visible">True</property>
                <child>
                  <widget class="GtkButton" id="add">
                    <property name="label" translatable="yes">Click to 
add </property>
                    <property name="visible">True</property>
                    <property name="can_focus">True</property>
                    <property name="receives_default">True</property>
                    <signal name="clicked" handler="on_add_clicked"/>
                  </widget>
                  <packing>
                    <property name="position">0</property>
                  </packing>
                </child>
                <child>
                  <widget class="GtkButton" id="search">
                    <property name="label" translatable="yes">click to 
search</property>
                    <property name="visible">True</property>
                    <property name="can_focus">True</property>
                    <property name="receives_default">True</property>
                    <signal name="clicked" handler="on_search_clicked"/>
                  </widget>
                  <packing>
                    <property name="position">1</property>
                  </packing>
                </child>
              </widget>
              <packing>
                <property name="position">2</property>
              </packing>
            </child>
            <child>
              <widget class="GtkVBox" id="vbox9">
                <property name="visible">True</property>
                <child>
                  <widget class="GtkLabel" id="label3">
                    <property name="visible">True</property>
                  </widget>
                  <packing>
                    <property name="position">0</property>
                  </packing>
                </child>
                <child>
                  <widget class="GtkButton" id="quit">
                    <property name="label" translatable="yes">Quit</property>
                    <property name="visible">True</property>
                    <property name="can_focus">True</property>
                    <property name="receives_default">True</property>
                    <signal name="clicked" handler="on_quit_clicked"/>
                  </widget>
                  <packing>
                    <property name="position">1</property>
                  </packing>
                </child>
              </widget>
              <packing>
                <property name="position">3</property>
              </packing>
            </child>
          </widget>
          <packing>
            <property name="position">1</property>
          </packing>
        </child>
      </widget>
    </child>
  </widget>
</glade-interface>

Save this code with Extension as .glade and this filename should be included in Python programing

Now my PYTHON PROGRAM follows


import sys
import gnome.ui
try:
  import pygtk
  pygtk.require("2.16")

except:
  pass 

try:
  import gtk
  import gtk.glade

except:
   print("GTK not available .. sorry")
   sys.exit(1)

class reader:
  
      def __init__(self , value ):
               
               self.value = value
               f1 = open("contacts.txt","r")
               for line in f1:
                  if value in line:
                    self.result = line
                            
      def filevalues(self):
             
                  return(self.result)

                

class mainLoop:
                widTree = None
                def __init__(self):
                     self.widTree = gtk.glade.XML("gladeinitiative.glade")

                     dic = {
			"on_search_clicked" : self.read ,
                         "on_add_clicked"  :  self.addcon ,
                         
                         "on_quit_clicked"  : self.end 
		            }

                     self.widTree.signal_autoconnect( dic )
                     self.widTree.get_widget("window1").show()
                
		       
                
                def read(self , widget ):
                  try:
                      thistime = reader(self.widTree.get_widget("entry1").get_text() )
                      self.widTree.get_widget("entry2").set_text(thistime.filevalues())
                  except AttributeError:
                     
                     self.widTree.get_widget("entry2").set_text("Name is not available !!!" )                  
               
 
           
 

                def addcon(self , widget):
                       a = self.widTree.get_widget("entry3").get_text()
                                            
                       f2 = open("contacts.txt","a")
                       f2.write(a)
                       f2.write("\n")
                       f2.close
                       return 0
                def end(self, widget):
		      sys.exit(0)

if __name__ == "__main__":
  letsdothis = mainLoop()
  gtk.main()
  print "main"
            
       
 


       
 

Then you run this script , You can use it as Contact book or Dirctory 😉

Screenshot is


Regards

sathia

Advertisements

About sathia

Web developer at cloudmint
This entry was posted in programs. Bookmark the permalink.

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 )

Google+ photo

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

Connecting to %s