Friday, December 11, 2009

a Simple paint application in python


hi
I'm back .I started my quest with Python from last week.Python is one of the coolest OOP scripting language.("no wonder google bots are programmed via Python").
It has a wide range of libraries Tkinter,wxWindget etc.These support a heck lot of GUI programming.These are lllr to GTK+ in UNIX.Remember the library that supports GUI in UNIX c.or the windows.h in VC++.the power of python is that its used for scientific computing cos the precision is perfect event for 1 millionth decimal in floating point.unlike in c/c++ where the long long int can support up to 16 digits(!!! don't know exactly).Python can support any no. of digits.hence its used by NASA for coding purpose.

Hence i decide to add one of my code for GUI programming its a paint application in python.
hope u would have installed python i n Linux environment.or in case of windows install it from
http://www.python.org/download/.




the code is

from Tkinter import *

class PaintBox( Frame ):
"""Demonstrate drawing on a Canvas"""

def __init__( self ):
"""Create Canvas and bind paint method to mouse dragging"""

Frame.__init__( self )
self.pack( expand = YES, fill = BOTH )
self.master.title( "A simple paint program" )
self.master.geometry( "300x150" )

self.message = Label( self,
text = "Click and Drag the mouse to draw" )
self.message.pack( side = BOTTOM )

# create Canvas component
self.myCanvas = Canvas( self )
self.myCanvas.pack( expand = YES, fill = BOTH )

# bind mouse dragging event to Canvas
self.myCanvas.bind( "", self.paint )

def paint( self, event ):
"""Create an oval of radius 4 around the mouse position"""

x1, y1 = ( event.x - 4 ), ( event.y - 4 )
x2, y2 = ( event.x + 4 ), ( event.y + 4 )
self.myCanvas.create_oval( x1, y1, x2, y2, fill = "black" )

def main():
PaintBox().mainloop()

if __name__ == "__main__":
main()


guys the most important part is check for indentations .python is strict to it and punishes those who don't follow it .just copy paste this code and add on specific functionalities to it.happy python coding


No comments:

Post a Comment