Showing posts with label function and loops in python. Show all posts
Showing posts with label function and loops in python. Show all posts

Wednesday, July 29, 2015

Python Tkinter Programming

Desktop GUI application creation in an easy mode is one of the specialty of the python programming. Python 2.7 and Python 3 both have an inbuilt class tkinter which is needed to program graphical interface in python. There are other modules as well but here we will focus only on tkinter.

A basic tkinter program to show 'Hello World' in GUI mode -

from Tkinter import *
# create a root window
root = Tk()
root.title("Lazy Buttons")
root.geometry("400x120")
lbl=Label(root,text="Hello World").pack()

root.mainloop()

If this shows error, change the Tkinter to tkinter in the import statement. After importing the module we create the root window by defining is as the Tk() object. In the next statement we named the window as "Lazy Buttons" by calling the root.title() method.
root.geometry() is used to define the width and height of the window. It has a formal notation like root.geometry("w x h+ x+ y") where w and h are for width and height respectively and the x and y are the coordinates of the screen.

In the next statement we created a Label, which will show the "Hello World" texts to be displayed  in the root window. The Label creator method takes the window name as input i.e. root and the text as input.The pack() method is needed to pack the label to the root window. The mainloop is needed to make the window to exist forever until you click the cross button.
Buy One Plus Two without Registration

Tuesday, July 14, 2015

Introduction to function - Python

Function - if you are familiar with any programming language, you should know what the function is. It is a process of minimizing code repetition. Of we have to do something frequently in a program we write the code snippet as function and call out when necessary.
Python function is declared by def and then followed by name of the function and parameters which the function needs, then followed by colon.
def add (a,b):
   c=a+b
   return c
while True:
   a=int(raw_input("please enter the first value or press x to quit"))
   b=int(raw_input("please enter the first value  or press x to quit"))
   if a=="x" or b=='x':
      break
   c=add(a,b)
   print c
  

Can you find the fault in the program? the a=='x' can not be executed as the conversion of x into integer can not be possible. So, the idle throws exception but also get out us of the loop.

you can also give default value to function input values. Lets see it.

def showname(name="Not Supplied",age=0):
    if name=="Not Supllied":
        print "Please supply the name"
        print "your age is =%d"%age
    elif age==0:
        print "Hello %s"%(name)
        print "You did not supplied the age, common don't be ashamed of your age"
    elif name=="Not Supplied" and age==0:
        print "Please supply the name and age"
    else:
        print "Hello",name," \n Your age is only ",age

while 1:
   
    name=raw_input("Press ctrl+c to quit. \nplease enter your name :")
    age =raw_input ("please enter your age :" )
    if name=="":
        showname(age)
    elif age=="":
        showname(name)
    elif age=="" and name=="":
        showname()
    else:
        showname(name,age)
   
This program contains some logical mistakes , can you identify them? if not see my blog about loops and conditionals.

  

Feautured Post

Python Trivia Post: enumerate()

Python has a very useful inbuilt function, which is called enumerate(). This is needed whenever you need the numbering of a certain element...