Notepad development---The use of tkinter
Last Update:2018-07-26
Source: Internet
Author: User
#coding: utf-8
__author__ = 'xshengjing'
from Tkinter import *
from tkMessageBox import *
from tkFileDialog import *
import os
file_name = ""
def search ():
top_search = Toplevel (root)
# top_search.geometry ("300 * 30 + 20 + 250")
label_01 = Label (top_search, text = "Find")
label_01.grid (row = 0, column = 0, padx = 5)
entry_01 = Entry (top_search, width = 20)
entry_01.grid (row = 0, column = 1, padx = 5)
button_01 = Button (top_search, text = "Find")
button_01.grid (row = 0, column = 2)
def anthor ():
showinfo ("Author Information", "This software is completed by Xiao Xu")
def about ():
showinfo ("Copyright Information.Copyright", "The copyright of this software belongs to Xiaoxu")
def open_file ():
global file_name
file_name = askopenfilename (defaultextension = ". txt")
if file_name == "":
file_name = None
else:
root.title ("FileName:" + os.path.basename (file_name))
text_pad.delete (1.0, END)
f = open (file_name, "r")
text_pad.insert (1.0, f.read ())
f.close ()
def cut ():
text_pad.event_generate ("<< Cut >>")
def copy ():
text_pad.event_generate ("<< Copy >>")
def paste ():
text_pad.event_generate ("<< Paste >>")
def redo ():
text_pad.event_generate ("<< Redo >>")
def undo ():
text_pad.event_generate ("<< Undo >>")
def select_all ():
text_pad.tag_add ('sel', '1.0', END)
def new ():
global file_name
root.title ("Untitled file")
file_name = None
text_pad.delete (1.0, END)
def save ():
global file_name
try:
f = open (file_name, "w")
msg = text_pad.get (1.0, END)
f.write (msg)
f.close ()
except:
save_as ()
def save_as ():
f = asksaveasfilename (initialfil = "untitled.txt", defaultextension = ". txt")
global file_name
file_name = f
fh = open (f, "w")
msg = text_pad.get (1.0, END)
fh.write (msg)
fh.close ()
root.title ("FileName:" + os.path.basename (f))
root = Tk ()
root.title ("Xiaoxu Notepad")
# root.geometry ("500 * 500 + 100 + 100")
menubar = Menu (root)
# create a pulldown menu, and add it to the menu bar
filemenu = Menu (menubar, tearoff = 0)
filemenu.add_command (label = "New", accelerator = "Ctrl + N", command = new)
filemenu.add_command (label = "Open", accelerator = "Ctrl + N", command = open_file)
filemenu.add_command (label = "Save", accelerator = "Ctrl + S", command = save)
filemenu.add_command (label = "Save As", accelerator = "Ctrl + Shift + S", command = save_as)
filemenu.add_command (label = "quit", command = root.quit)
menubar.add_cascade (label = "file", menu = filemenu)
# create more pulldown menus
editmenu = Menu (menubar)
editmenu.add_command (label = "Undo", accelerator = "Ctrl + Z", command = undo)
editmenu.add_command (label = "Redo", accelerator = "Ctrl + Y", command = redo)
editmenu.add_separator ()
editmenu.add_command (label = "cut", accelerator = "Ctrl + X", command = cut)
editmenu.add_command (label = "copy", accelerator = "Ctrl + C", command = copy)
editmenu.add_command (label = "paste", accelerator = "Ctrl + V", command = paste)
editmenu.add_separator ()
editmenu.add_command (label = "Find", accelerator = "Ctrl + F", command = search)
editmenu.add_command (label = "select all", accelerator = "Ctrl + A", command = select_all)
menubar.add_cascade (label = "edit", menu = editmenu)
aboutmenu = Menu (menubar, tearoff = 0)
aboutmenu.add_command (label = "author", command = anthor)
aboutmenu.add_command (label = "copyright", command = about)
menubar.add_cascade (label = "About", menu = aboutmenu)
root.config (menu = menubar)
toolbar = Frame (root, height = 25, bg = "light sea green")
short_button = Button (toolbar, text = "Open", command = open_file)
short_button.pack (side = LEFT, padx = 5, pady = 5)
short_button = Button (toolbar, text = "Save")
short_button.pack (side = LEFT)
toolbar.pack (expand = NO, fill = X)
status = Label (root, text = "Ln20", bd = 1, relief = SUNKEN, anchor = W)
status.pack (side = BOTTOM, fill = X)
#Show line number
lnlabel = Label (root, width = 2, bg = 'antique white')
lnlabel.pack (side = LEFT, fill = Y)
text_pad = Text (root, undo = True)
text_pad.pack (expand = YES, fill = BOTH)
scroll = Scrollbar (text_pad)
text_pad.config (yscrollcommand = scroll.set)
scroll.config (command = text_pad.yview)
scroll.pack (side = RIGHT, fill = Y)
root.mainloop ()