Text Editor

Topics requirements:

Notepad style application that can open, edit, and save text documents. 

Add syntax highlighting and other features.

The following code implements questions asked of the first line

The second line is not achieved questions asked (syntax highlighting). Do not have time, welcome to comment

Import Tkinter
 from Tkinter Import *
 from tkinter.ttk Import Scrollbar 

root = tkinter.Tk ()   # object-oriented class, create a GUI object 
root.title ( " TextEditor " )   # window name 
root.geometry ( " 600x400 " )   # window size 
enter_file = tkinter.Entry (the root)   # object-oriented class, creating an input box in the GUI 
enter_file.pack ()   # place input box (essential statements) 
Word = tkinter.Text (the root, Use the undo = True)    # Object class to create a display box in the GUI 
word.pack () 
scroll_barThe Scrollbar = (the root)   # an object class to create a scroll bar in the GUI 
scroll_bar.config (Command = word.yview)   # associated with a display frame scrollbar 
word.config (yscrollcommand = scroll_bar.set, width = 20, height 20 is =, = background ' #FFFFFF ' ) 
word.pack (Side = the LEFT, Fill = BOTH, the expand =. 1 ) 
scroll_bar.pack (Side = RIGHT, Fill = the Y) 
MENU = tkinter.Menu (the root)   # object class of creating a menu bar in the GUI 
root.config (the mENU = the mENU)    # associated 

DEF OpenFile (): 
    file_path = enter_file.get ()   # get input box content 
    with Open (file_path) AS fp: 
        contentFp.read = ()   # read files 
        word.delete (0.0, tkinter.END)   # empty display frame 
        word.insert (tkinter.END, Content)   # display read file 


DEF saveFile (): 
    Content = word.get (0.0 , tkinter.END) 
    file_path = enter_file.get () 
    with Open (file_path, ' w ' ) AS fw: 
        fw.write (content)    # displays the contents of the text box to save all of the original document, which is editing 


def Use the undo (): 
    word.edit_undo ()   # undo the previous operation function 


button_o = tkinter.Button (the root, text = ' Open ', Command = OpenFile)   # operation after setting button click tag 
button_s = tkinter.Button (the root, text = ' Save ' , Command = saveFile) 
button_o.pack () 
button_s.pack () 
menu.add_command (label = ' revocation ' , the Command = Use the undo)   # Sets the menubar tag and a corresponding action 

root.mainloop ()   # necessary statements into an infinite loop so that the window has been displayed 


# invoke: 
# enter the full txt file open in the new GUI interface absolute path, file name suffix do not forget! 
# Can be realized: open, edit, undo, save function

 

Guess you like

Origin www.cnblogs.com/YlnChen/p/12588425.html