Python Development Simple Notepad

Source: Internet
Author: User

Summary:

This article is a simple Notepad developed using Python, combined with tkinter.

The operating environment of this article: ubuntu,python2.7, using Pycharm for code editing, the individual likes its code auto-completion function.

I want to learn a bit more about Python and try to do something today using python, combined Tkinter to make a simple cross-platform Notepad. The final implementation of the Notepad as follows, is also a very perfectly formed, and then will continue to improve:

For example, we can see that this Notepad is mainly divided into three modules: file, edit and about, combine my own habits plus four toolbar: new, open, Undo and save. Down on my personal build this notepad to do a summary.

A holistic framework built

1. Establishment of three main modules

first, we set up the three main modules in the module, and then set up the functions of each module. First take the file as an example: under the function: New, open, save and Save As, the code is as follows:

#-*-encoding:utf8 fromTkinterImport*Root=Tk () root.title ('Benben Node')#Create Menumenubar =menu (Root) root.config (menu=menubar) Filemenu=Menu (MenuBar) filemenu.add_command (label='New', accelerator ='Ctrl + N') Filemenu.add_command (label='Open', accelerator ='Ctrl + O') Filemenu.add_command (label='Save', accelerator ='Ctrl + S') Filemenu.add_command (label='Save As', accelerator ='Ctrl + Shift + S') menubar.add_cascade (label='file', menu =filemenu) Root.mainloop ()

Operation Result:

We can see that a simple file bar is built. corresponding to build edits and about modules. Edit refers to the text editor to use the module, under the function: Undo, Redo, copy, cut, Paste, find and select all, about is under the author and copyright two columns. The corresponding module corresponds to the following code:

#EditEditmenu =Menu (MenuBar) editmenu.add_command (label='Revoke', accelerator ='Ctrl + Z') Editmenu.add_command (label='Redo', accelerator ='Ctrl + Y') Editmenu.add_command (label='Copy', accelerator ='Ctrl + C') Editmenu.add_command (label='Cut', accelerator ='Ctrl + X') Editmenu.add_command (label='Paste', accelerator ='Ctrl + V') Editmenu.add_command (label='Find', accelerator ='Ctrl + F') Editmenu.add_command (label='Select All', accelerator ='Ctrl + A') menubar.add_cascade (label='Edit', menu =Editmenu)#about theAboutmenu =Menu (MenuBar) aboutmenu.add_command (label='author') Aboutmenu.add_command (label='Copyright') menubar.add_cascade (label='about the', menu = Aboutmenu)

Operation Result:

Failed to get the function corresponding to each module.

2. Toolbar

add toolbar in Notepad, add the corresponding button and set the appropriate spacing, the corresponding code is as follows:

#toolbar
toolbar = Frame (root,height = 15,BG = ' Skyblue ')
Shortbutton = Button (toolbar,text = ' new ', command = open)
Shortbutton.pack (side = left)
Shortbutton = Button (toolbar,text = ' open ', command = OpenFile)
Shortbutton.pack (side = LEFT,PADX = 5,pady = 5)
Shortbutton = Button (Toolbar,text = ' save ', command = Save)
Shortbutton.pack (side = right)
Shortbutton = Button (Toolbar,text = ' undo ', command = undo)
Shortbutton.pack (side = RIGHT,PADX = 5,pady = 5)
Toolbar.pack (expand = No,fill = X)

3. Create status bar (StatusBar) and body edit area

First add in the program:

root = Tk () root.title ('benben Node') root.geometry ("  800x500+100+100")

To edit the code for the status bar:

# StatusBar ' Ln20 ' ' W '  = Bottom,fill = X)

Of course we can also create the corresponding editing area and scroll bar, the corresponding code is as follows:

# Body Edit Area ' Antique White '  = Left,fill == Text (Root,undo == Yes,fill == = = = = Right,fill = Y)

Execute code, Result:

Enter the corresponding scroll bar that you can see corresponding to the right border with the input scrolling.

Two. About the implementation of the module

In these three modules, about is the best implementation of the one, then from the simplest to start to achieve it. The corresponding code is as follows:

 fromTkmessageboxImport*#about thedefauthor (): Showinfo ('Author Information','This software is done by cute Ben Ben')defAbout (): Showinfo ('copyright information.','copyright belongs to Ben Ben') Aboutmenu=Menu (MenuBar) aboutmenu.add_command (label='author', command =author) aboutmenu.add_command (label='Copyright', command =About ) Menubar.add_cascade (label='about the', menu = Aboutmenu)

Operation Result:

     

Three. Implementation of the file module

This part of the implementation of the Tkinter official website in the filedialogs, the corresponding use of various methods to implement the function: New, open, save and Save as, the corresponding function implementation, the code is as follows:

#Newdefnew (): Root.title ('File not named') filename=None Textpad.delete (1.0, END)#OpendefOpenFile ():Globalfilename filename= Askopenfilename (defaultextension ='. txt')    iffilename = ="': FileName=NoneElse: Root.title ('FileName:'+os.path.basename (filename)) Textpad.delete (1.0, END) F= open (filename,'R') Textpad.insert (1.0, F.read ()) F.close ()#SavedefSave ():GlobalfilenameTry: F= open (filename,'W') msg= Textpad.get (1.0, END) f.write (msg) f.close ()except: SaveAs ()#Save AsdefSaveAs (): F= Asksaveasfilename (initialfile='not named. txt', defaultextension='. txt')    Globalfilename filename=F FH= Open (F,'W') msg= Textpad.get (1.0, END) fh.write (msg) fh.close () Root.title ('FileName:'+os.path.basename (f))

The corresponding command is added to the Filemenu and toolbar, and the result of operation is as follows:

  

Implementation of the four editing modules

Edit the functions to be implemented: Undo, Redo, copy, cut, Paste, find and select all, corresponding function code is as follows:

defcut (): Textpad.event_generate ('<<Cut>>')defcopy (): Textpad.event_generate ('<<Copy>>')defpaste (): Textpad.event_generate ('<<Paste>>')defRedo (): Textpad.event_generate ('<<Redo>>')defundo (): Textpad.event_generate ('<<Undo>>')defSelectAll (): Textpad.tag_add ('sel','1.0', END)defsearch (): Topsearch=toplevel (Root) topsearch.geometry ('300x30+200+250') Label1= Label (topsearch,text='Find') Label1.grid (Row=0, column=0,padx=5) Entry1= Entry (topsearch,width=20) Entry1.grid (Row=0, column=1,padx=5) Button1= Button (topsearch,text='Find') Button1.grid (Row=0, column=2)

Also add the corresponding command in Editmenu and toolbar. Run code, feature implementation! Shortcut key Pro Test available ~

This is a summary of the development of a simple notepad, then there will be time to add more features.

Python Development Simple Notepad

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.