My First Code in Python

My first code in python deals with database management system,using a module called gtk which helps in building a graphical user interface.The thing that made me use python,was that I had no idea of coding before and seniors (godlike 🙂 ) had told me its the simplest and probably it would help me understand C better which is being taught this semester in college.

Basically it consists of a enter password window,and another window with various options such as ‘Enter Instrument’ which store the entries to a text file.The entries can be retrieved directly using the ‘Search Instrument’ option.

For the ‘change password’,it writes it to a text file and retrieves it  when the password is entered

This is the sample code for my main window

import pygtk
pygtk.require('2.0')
import gtk

class backstage:
    def __init__(self):
      self.window=gtk.Window(gtk.WINDOW_TOPLEVEL)
      self.window.set_title('window')
      self.window.connect('destroy',self.destroy)
      self.window.set_size_request(500,500)
      self.window.set_title('Backstage')

      self.box=gtk.VBox()
      self.button1=gtk.Button('Enter instrument')
      self.button1.connect('clicked',self.but_call1)
      self.button2=gtk.Button('Search instrument')
      self.button2.connect('clicked',self.but_call)
      self.button3=gtk.Button('Giving instruments')
      self.button3.connect('clicked',self.but_call3)
      self.button4=gtk.Button('Returning instruments')
      self.button4.connect('clicked',self.but_call4)
      self.button5=gtk.Button('Change password')
      self.button5.connect('clicked',self.but_call)
      self.exit=gtk.Button('Exit')
      self.exit.connect('clicked',lambda w:gtk.main_quit())

      self.box.pack_start(self.button1)
      self.box.pack_start(self.button2)
      self.box.pack_start(self.button3)
      self.box.pack_start(self.button4)
      self.box.pack_start(self.button5)
      self.box.pack_start(self.exit)
      self.window.add(self.box)
      self.window.show_all()

And this is for the enter password window

</pre>
def __init__(self):
<pre>    self.window=gtk.Window(gtk.WINDOW_TOPLEVEL)
    self.window.set_title('Enter Password')
    self.window.set_size_request(300,300)
    self.table=gtk.Table(40,40,False)
    self.entry=gtk.Entry()
    self.entry.set_visibility(False)

    self.button=gtk.Button('OK')
    self.button.connect('clicked',self.ok)

    self.table.attach(self.entry,0,40,0,15)
    self.table.attach(self.button,15,25,20,30)
    self.window.add(self.table)
    self.window.show_all()

  def ok(self,widget):
    a=self.entry.get_text()
    c=open('/home/manoj/ch_p.txt','r+')
    b=c.read()

    c.close()
    l=len(b)
    d=b.split()[0]

    if a == d:
      self.window.destroy()
      backstage()
    else:
      self.box=gtk.VBox()
      self.label=gtk.Label('Better luck next time loser')
      self.label1=gtk.Label('Please close the window')
      self.table.destroy()
      self.box.pack_start(self.label)
      self.box.pack_start(self.label1)
      self.window.add(self.box)
      self.window.show_all()

here backstage is the main class which is not shown here due to lack of space and ch_p.txt is the text file in which the
changed password is entered

For the entire code refer to this doc  inventory

Gtk is not an inbuilt module and is used mainly for GUI designing and is ‘lite’ as seniors describe it.The code may seem long but there is not much to think except about how to position the buttons and stuff.Hope I learn other awesome modules like os and urllib,which form the crux of python.Till then,bye..

Advertisements

6 comments

  1. fictious man · · Reply

    Which books are you using to learn the Python ???

    or which tutorials are you referring to ??

    1. Hey ive just started learning,there are some awesome tutorials by parlante and bucky roberts on youtube,and im planning to read the entire documentation once before i proceed any further.

  2. Manoj! Nice post! you should add more tags like python, gtk , GUI, etc! It helps.
    And add to get the code in worpress theme.

    your code here..
    

    for example see my blog. http://jayrambhia.wordpress.com

    1. ok.. my bad..
      sourcecode language = “python”
      in square brackets
      then your code here
      /sourcecode in square brackets

    2. Thanks Jay,ill do this the next time

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

%d bloggers like this: