Using Python development to implement a simple Notepad _python

Source: Internet
Author: User
Tags pack


Objective



This article's operating environment: ubuntu,python2.7, uses is Pycharm carries on the code to edit, the individual likes its code automatically to complement the function.



Sample diagram






As shown above, we can see that this Notepad is divided into three modules: file, edit and about, combine my own custom plus four toolbar: new, open, Undo and save.



Come down to my personal build this notepad to make a summary.



I. Overall framework Construction



1. Establishment of three main modules



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

#-*-encoding: utf8
from Tkinter import *

root = Tk ()
root.title ('Benben Node')

#create menu
menubar = 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 column is built. Corresponding to build editing and about modules. Editing refers to the modules to be used in text editing. The following functions are provided: undo, redo, copy, cut, paste, search and select all; about the author and copyright columns. The corresponding code of the corresponding module is as follows:

#edit
editmenu = Menu (menubar)
editmenu.add_command (label = 'Undo', 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)

#on
aboutmenu = Menu (menubar)
aboutmenu.add_command (label = 'Author')
aboutmenu.add_command (label = 'Copyright')
menubar.add_cascade (label = 'About', menu = aboutmenu)
operation result:



The screenshot failed to get the corresponding function of each module.

2. toolbar

Add toolbar in Notepad, add corresponding button and set proper 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 a status bar (statusbar) and text editing area

First add in the program:

root = Tk ()
root.title ('Benben Node')
root.geometry ("800x500 + 100 + 100")
Edit the code of the status bar:

#statusbar
status = Label (root, text = 'Ln20', bd = 1, relief = SUNKEN, anchor = 'w')
status.pack (side = BOTTOM, fill = X)
Of course, we can also create the corresponding editing area and scroll bar, the corresponding code is as follows:

#Text editing area
lnlabel = Label (root, width = 2, bg = 'antique white')
lnlabel.pack (side = LEFT, fill = Y)

textpad = Text (root, undo = True)
textpad.pack (expand = YES, fill = BOTH)

scroll = Scrollbar (textpad)
textpad.config (yscrollcommand = scroll.set)
scroll.config (command = textpad.yview)
scroll.pack (side = RIGHT, fill = Y)
Executing the code, the result:



Input correspondingly, you can see that the scroll bar corresponding to the right border scrolls with the input.

Second, about the realization of the module

Among the three modules, the one that is the best to achieve, then start from the simplest.

The corresponding code is as follows:

from tkMessageBox import *

#on
def author ():
  showinfo ('Author information', 'This software is completed by cute Benben')
def about ():
  showinfo ('Copyright information.copyright', 'Copyright belongs to Benben')

aboutmenu = Menu (menubar)
aboutmenu.add_command (label = 'Author', command = author)
aboutmenu.add_command (label = 'Copyright', command = about)
menubar.add_cascade (label = 'About', menu = aboutmenu)
operation result:



Three, the realization of the file module

For the implementation of this part, please refer to the fileDialogs in Tkinter's official website. The corresponding methods are used to implement the functions: new, open, save and save as, and the corresponding function implementation is established.

#New
def new ():
  root.title ('Unnamed file')
  filename = None
  textpad.delete (1.0, END)
#turn on
def openfile ():
  global filename
  filename = askopenfilename (defaultextension = '.txt')
  if filename == '':
    filename = None
  else:
    root.title ('FileName:' + os.path.basename (filename))
    textpad.delete (1.0, END)
    f = open (filename, 'r')
    textpad.insert (1.0, f.read ())
    f.close ()

#save
def save ():
  global filename
  try:
    f = open (filename, 'w')
    msg = textpad.get (1.0, END)
    f.write (msg)
    f.close ()
  except:
    saveas ()
#Save as
def saveas ():
  f = asksaveasfilename (initialfile = 'Unnamed.txt', defaultextension = '. txt')
  global filename
  filename = f
  fh = open (f, 'w')
  msg = textpad.get (1.0, END)
  fh.write (msg)
  fh.close ()
  root.title ('FileName:' + os.path.basename (f))
Correspondingly add the corresponding command in the filemenu and toolbar, the result of the operation:



Fourth, the realization of the editing module

The functions to be realized by the editor: undo, redo, copy, cut, paste, search and select all, the corresponding function codes are as follows:

def cut ():
  textpad.event_generate ('<< Cut >>')

def copy ():
  textpad.event_generate ('<< Copy >>')

def paste ():
  textpad.event_generate ('<< Paste >>')

def redo ():
  textpad.event_generate ('<< Redo >>')

def undo ():
  textpad.event_generate ('<< Undo >>')

def selectAll ():
  textpad.tag_add ('sel', '1.0', END)

def search ():
  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 corresponding commands in editmenu and toolbar. Run the code, the function is realized! Shortcut key pro test available ~

to sum up

The above is all the content in this article. This is a summary of the simple notepad developed this time, and more functions will be added later. I hope that the content of this article can bring some help to everyone's study or work. If you have any questions, you can leave a message to communicate.

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.