Python Project Final HD

Post on 12-Jan-2016

29 views 3 download

Tags:

description

CBSE Python project

transcript

Page

NotescribeAn In ter faced Text Edi tor m ade in Python.

By Arvind Sr inivasan

Contents1 Acknowledgement . . . . . . . . . . . . . . . . . . . . 01

2 About Notescr ibe . . . . . . . . . . . . . . . . . . . . 02

3 Program St ructure . . . . . . . . . . . . . . . . . . . . 03

4 Source Code . . . . . . . . . . . . . . . . . . . . 07

5 Output . . . . . . . . . . . . . . . . . . . . 12

6 Conclusions and Bibl iography

. . . . . . . . . . . . . . . . . . . . 16

Every work accom pl ished is a pleasure - a sense of sat isfact ion. H owever a num ber of people m otivate, cr i t icize and appreciate a work with their object ives, ideas and opin ions. H ence, I take th is oppor tun i ty to thank al l , who have dir ect ly or indir ect ly helped m e to accom pl ish th is project.

- Fir st ly, I would l ike to thank m y computer science teacher , M r s Pavithr a wi thout whose suppor t th is

project would not have been com plete. Next, I would l ike to thank al l the people who gave their valuable t im e, cooperat ion and feedback to th is project. I would also l ike to thank m y school for providing m e with the resources which beyond any doubt have helped m e.

- Secondly, I would l ike to sincerely thank our Pr incipal V.Thiagar a jan and Vice Pr incipal, M r s. Lakshmi Kannan for their guidance throughout.

- Thir dly, I thank m y class teacher, M rs M ytheree for helping us f in ish our project in t im e.

Last but not the least, I would want to thank m y parents and other facul ty m em bers for standing beside m e to m ake th is project a wholesom e one.

A ck now ledgement

1

Notescribe app

Working with text has never been easier.

Open Source Formattable User Friendly

2

Program StructureSeveral Python concepts have been used to create th is project.These pr im ar i ly include the fol lowing:

Cl asses:

Tkinter Class:

Tk inter suppor ts 15 core widgets out of which the fol lowing few have been instrum ental in the design of the program . They are:

- Bu t ton : used to execute a com m and or other operat ion.

- Can vas: Structured graphics. This widget can be used to

draw graphs and plots, create graphics edi tor s, and to im plem ent custom widgets.

- En t r y: A text entr y f ield.

- Fr am e: A container widget. The fr am e can have a border

and a background, and is used to group other widgets when creat ing an appl icat ion or dialog layout.

- Label : Displays a text or an im age.

- M en u : A m enu pane. Used to im plem ent pul l -down and

pop-up m enus.- M essage: Display a text. Sim i lar to the label widget, but can

autom atical ly wrap text to a given width or aspect r at io.- Scal e: Al lows you to set a num er ical value by dragging a

3

?sl ider?.- Scr ol l bar : Standard scrol lbars for use with canvas, entr y,

l istbox and text widgets.- Text : Form atted text display. Al lows you to display and edi t

text wi th var ious styles and attr ibutes. Also suppor ts em bedded im ages and windows.

- Pan edW i n dow : A container widget that organizes chi ld

widgets in r e-sizable panes.

Quitter Class:

This class cal ls the qu i t () funct ion when the qui t button is

cl icked. The sub-funct ion of the m odule is designed to ver i fy whether the user r eal ly wanted to qui t .

Scr olledText Class:

This class does the funct ion of generat ing a GUI layer over the root layer. I t is the class where com m on m ajor funct ions of the text edi tor are added.

A few sub-funct ions are:

- __i n i t__(): In i t iate Scrol ledText class. Fram e Bui lding and

configurat ion is done under th is funct ion which cal ls fr om i ts other funct ions.

- m ak ew i dgets(): A container widget that stores text is

created (Text ) which is then al igned and assigned the

values that would be typed.- set text (): This funct ions is used to pinpoint the locat ion of

the text and providing basic funct ional i t ies of a text edi tor l ike delet ing and inser t ing character s using focus() which

enables proper poin ter posi t ion.

4

- get text (): Returns text fr om star t to f in ish using tags.

SimpleEditor Class:

This class is a der ived sub-class fr om Scrol ledText where al l the m odif icat ions and creat iveness l ies. To m ake th is GUI m ore attr act ive and appeal ing, Var ious techniques and tr icks had been fol lowed. This is also the place for adding a lot m ore funct ions, thus increasing the overal l funct ional i ty and usabi l i ty.

This inher itance of classes can be considered as being

M ul t i l evel .

For m at t i n g Fun ct i on s:- on Save(): Asks save_as_f i l en am e and copies al l text

wi thout form att ing and wr i tes them to the text f i le using

w r i te() funct ion.

- on Cu t(): Select r equired text using tag indexes and delete

the selected text whi le using tk in ter to access cl ipboard and append the data selected fr om the given text.

Scrol ledText

SimpleEditor

5

- on Paste(): Repeat sam e process as above but instead of

delet ing, paste the select ion stored in the cl ipboard.- on I tal i c(), on Bol d (), on Str i k e(), on Un der l i n e(),

on H i gh l i gh t ():Select any given text using tags on m ouse

pointer s and use text.tag_con f i g() to configure as you

want, form att ing the selected text.

Other uses of con cepts:

Text fi les:

Text f i le is a user r eadable f i le that contains in form ation in a tr anslated form at un l ike the way i t is held in m em ory. For saving of notes and other r eadi ly avai lable to r ead docum ents, text f i les are used. funct ions l ike wr i te() have been used to som e extent whi le creat ing th is program .

M odu l es used :- Tk in ter- os- sys- tkSim pleDialog- tkFi leDialog- tkM essageBox

6

Program Code# - * - c odi ng: ut f - 8 - * -

f r om Tk i nt er i mpor t *

f r om t k Si mpl eDi al og i mpor t as k s t r i ng

f r om t k Fi l eDi al og i mpor t as k s av eas f i l ename

f r om t k Mes s ageBox i mpor t as k ok c anc el

r oot = Tk ( )

r oot . t i t l e( " NOTESCRI BE" )

TI TLE=' NOTESCRI BE'

r oot . c onf i g( bac k gr ound=" #FFFFFF" )

r oot . wm_s t at e( " z oomed" )

m1 = PanedWi ndow( )

m1. c onf i g( bd=10, bg=" #FFFFFF" , wi dt h=200)

m1. pac k ( f i l l =BOTH, ex pand=1)

t op = Label ( m1, t ex t =" Wel c ome t o NOTESCRI BE - A di s t r ac t i on- f r ee t ex t edi t or - Pr ogr am by Ar v i ndc heenu. " , f ont =( " Hel v et i c a Neue" , 20, ' bol d' ) , f or egr ound=' bl ac k ' , bac k gr ound=' whi t e' )

m1. add( t op)

c l as s Qui t t er ( Fr ame) :

def __i ni t __( s el f , par ent =None) :

Fr ame. __i ni t __( s el f , par ent )

s el f . pac k ( )

def qui t ( s el f ) :

ans = as k ok c anc el ( ' Ver i f y ex i t ' , " Real l y qui t ?" )

i f ans : Fr ame. qui t ( s el f )

7

c l as s Sc r ol l edTex t ( Fr ame) :

def __i ni t __( s el f , par ent =None, t ex t =' ' , f i l e=None) :

Fr ame. __i ni t __( s el f , par ent )

s el f . pac k ( ex pand=YES, f i l l =BOTH)

s el f . c onf i g( bac k gr ound=' #FFFFFF' )

s el f . mak ewi dget s ( )

s el f . s et t ex t ( t ex t , f i l e)

def mak ewi dget s ( s el f ) :

t ex t = Tex t ( s el f , r el i ef =SUNKEN)

t ex t . pac k ( s i de=LEFT, ex pand=YES, f i l l =BOTH)

s el f . t ex t = t ex t

def s et t ex t ( s el f , t ex t =' ' , f i l e=None) :

i f f i l e:

t ex t = open( f i l e, ' r ' ) . r ead( )

s el f . t ex t . del et e( ' 1. 0' , END)

s el f . t ex t . i ns er t ( ' 1. 0' , t ex t )

s el f . t ex t . mar k _s et ( I NSERT, ' 1. 0' )

s el f . t ex t . f oc us ( )

def get t ex t ( s el f ) :

r et ur n s el f . t ex t . get ( ' 1. 0' , END+' - 1c ' )

s av e= Phot oI mage( f i l e=" s av e. gi f " )

c ut = Phot oI mage( f i l e=" c ut . gi f " )

pas t e = Phot oI mage( f i l e=" pas t e. gi f " )

bol d = Phot oI mage( f i l e=" bol d. gi f " )

i t al i c = Phot oI mage( f i l e=" i t al i c . gi f " )

s t r i k e = Phot oI mage( f i l e=" s t r i k e. gi f " )

hi ghl i ght = Phot oI mage( f i l e=" quot e. gi f " )

under l i ne = Phot oI mage( f i l e=" under l i ne. gi f " )

c l as s Si mpl eEdi t or ( Sc r ol l edTex t ) :

8

def __i ni t __( s el f , par ent =None, f i l e=None) :

f r m = Fr ame( par ent )

f r m. pac k ( f i l l =X, s i de=' r i ght ' )

f r m. c onf i g( bac k gr ound=" #FFFFFF" )

But t on( f r m, t ex t =' Sav e' , i mage= s av e, c ommand=s el f . onSav e, hi ghl i ght bac k gr ound=' #FFFFFF' , bg=' #FFFFFF' , f g=' bl ac k ' , r el i ef =' f l at ' ) . pac k ( s i de=BOTTOM, pady =5, padx =10)

But t on( f r m, t ex t =' Cut ' , i mage= c ut , c ommand=s el f . onCut , hi ghl i ght bac k gr ound=' #FFFFFF' , bg=' #FFFFFF' , f g=' bl ac k ' , r el i ef =' f l at ' ) . pac k ( s i de=BOTTOM, pady =5, padx =10)

But t on( f r m, t ex t =' Pas t e' , i mage= pas t e, c ommand=s el f . onPas t e, hi ghl i ght bac k gr ound=' #FFFFFF' , bg=' #FFFFFF' , f g=' bl ac k ' , r el i ef =' f l at ' ) . pac k ( s i de=BOTTOM, pady =5, padx =10)

But t on( f r m, t ex t =' I t al i c ' , i mage= i t al i c , c ommand=s el f . onI t al i c , hi ghl i ght bac k gr ound=' #FFFFFF' , bg=' #FFFFFF' , f g=' bl ac k ' , r el i ef =' f l at ' ) . pac k ( s i de=BOTTOM, pady =5, padx =10)

But t on( f r m, t ex t =' Bol d' , i mage= bol d, c ommand=s el f . onBol d, hi ghl i ght bac k gr ound=' #FFFFFF' , bg=' #FFFFFF' , f g=' bl ac k ' , r el i ef =' f l at ' ) . pac k ( s i de=BOTTOM, pady =5, padx =10)

But t on( f r m, t ex t =' St r i k et hr ough' , i mage= s t r i k e, c ommand=s el f . onSt r i k e, hi ghl i ght bac k gr ound=' ' #FFFFFF' , bg=' #FFFFFF' ' , f g=' bl ac k ' , r el i ef =' f l at ' ) . pac k ( s i de=BOTTOM, pady =5, padx =10)

But t on( f r m, t ex t =' Under l i ne' , i mage= under l i ne, c ommand=s el f . onUnder l i ne, hi ghl i ght bac k gr ound=' #FFFFFF' , bg=' #FFFFFF' , f g=' bl ac k ' , r el i ef =' f l at ' ) . pac k ( s i de=BOTTOM, pady =5, padx =10)

But t on( f r m, t ex t =' Hi ghl i ght ' , i mage= hi ghl i ght , c ommand=s el f . onHi ghl i ght , hi ghl i ght bac k gr ound=' #FFFFFF' , bg=' #FFFFFF' ' , f g=' bl ac k ' , r el i ef =' f l at ' ) . pac k ( s i de=BOTTOM, pady =5, padx =10]

Sc r ol l edTex t . __i ni t __( s el f , par ent , f i l e=f i l e)

s el f . t ex t . c onf i g(

bor der wi dt h=0,

f ont =" { Hel v et i c a Neue} 40" ,

f or egr ound=" #000000" ,

bac k gr ound=" #FFFFFF" ,

9

i ns er t bac k gr ound=" bl ac k " , # c ur s or

s el ec t f or egr ound=" whi t e" , # s el ec t i on

hi ghl i ght t hi c k nes s =0,

s el ec t bac k gr ound=" bl ac k " ,

wr ap= WORD, # us e wor d wr appi ng

undo=Tr ue,

wi dt h=64, )

def onSav e( s el f ) :

f i l ename = as k s av eas f i l ename( )

i f f i l ename:

al l t ex t = s el f . get t ex t ( )

open( f i l ename, ' w' ) . wr i t e( al l t ex t )

def onCut ( s el f ) :

t ex t = s el f . t ex t . get ( SEL_FI RST, SEL_LAST)

s el f . t ex t . del et e( SEL_FI RST, SEL_LAST)

s el f . c l i pboar d_c l ear ( )

s el f . c l i pboar d_append( t ex t )

def onPas t e( s el f ) :

t r y :

t ex t = s el f . s el ec t i on_get ( s el ec t i on=' CLI PBOARD' )

s el f . t ex t . i ns er t ( I NSERT, t ex t )

ex c ept Tc l Er r or :

pas s

def onI t al i c ( s el f ) :

s el f . t ex t . t ag_add( " y es wher e" , SEL_FI RST, SEL_LAST)

i f s el f . t ex t . t ag_c onf i g( ' her e' , f ont =( ' Hel v et i c a Neue' , 40, ' bol d' ) , f or egr ound=" bl ac k " ) :

s el f . t ex t . t ag_c onf i g( ' y es wher e' , f ont =( ' Hel v et i c a Neue' , 40, ' bol d i t al i c ' ) , f or egr ound=" bl ac k " )

el s e:

10

s el f . t ex t . t ag_c onf i g( ' y es wher e' , f ont =( ' Hel v et i c a Neue' , 40, ' i t al i c ' ) , f or egr ound=" bl ac k " )

def onBol d( s el f ) :

s el f . t ex t . t ag_add( " her e" , SEL_FI RST, SEL_LAST)

s el f . t ex t . t ag_c onf i g( ' her e' , f ont =( ' Hel v et i c a Neue' , 40, ' bol d' ) , f or egr ound=" bl ac k " )

def onSt r i k e( s el f ] :

s el f . t ex t . t ag_add( " nowher e" , SEL_FI RST, SEL_LAST)

s el f . t ex t . t ag_c onf i g( ' nowher e' , f or egr ound=" bl ac k " , ov er s t r i k e=1)

def onUnder l i ne( s el f ) :

s el f . t ex t . t ag_add( " t her e" , SEL_FI RST, SEL_LAST)

s el f . t ex t . t ag_c onf i g( " t her e" , f or egr ound=' bl ac k ' , under l i ne=1)

def onHi ghl i ght ( s el f ) :

s el f . t ex t . t ag_add( " wher e" , SEL_FI RST, SEL_LAST)

s el f . t ex t . t ag_c onf i g( " wher e" , bac k gr ound=" y el l ow" , f or egr ound=" bl ac k " )

i f __name__ == ' __mai n__' :

t r y :

Si mpl eEdi t or ( f i l e=s y s . ar gv [ 1] ) . mai nl oop( )

ex c ept I ndex Er r or :

Si mpl eEdi t or ( ) . mai nl oop( )

11

Output Screens

# 1

The com pl ete Text w i dget :

12

Quote:

Un der l i n e:

St r i k e th r ough:

13

# 2

# 3

# 4

I tal i c:

Cu t an d Paste:

14

# 5

# 6

Save Text docum en t :

# 7

15

H ence, the a python program was wr i t ten to dem onstrate a project wi th the use of f i les, classes and funct ions. This program features the carbon copy of a sim ple text edi tor that enables easy and fun f i l led wr i t ing.

I thank the school m anagem ent for giving us the golden oppor tun i ty to work on a program that is r eal ist ic and f i l led with fun. I thank our com puter science teacher, M rs.Pavi tr a who helped us and suppor ted us in m ak ing th is project a great success. I f inal ly thank the m anagem ent for giving us the perm ission to work in our labs and our parents who have suppor ted us and have been in the m ost ter r ible m om ents of our l i fe an guided us!

Conclusion

1

- Ncer t Python Lab M anual- Google and W ik ipedia.- usingpython.com- onl inepythontutor.com- codeacadem y.com- Coursera courses fr om Rice Universi ty.- Im ages fr om google.

Bibl iography

Thank You.Program m er : Arvind Sr in ivasanClass: 12 ASchool : Arsha Vidya M andir