# -*- coding: utf-8 -*-
from Tkinter import *
from tkMessageBox import *
from tkFileDialog import *
import os
filename=''
def author():
showinfo('作者信息','学习使用')
def about():
showinfo('python','学习使用')
def opens():
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()
def new():
global filename
root.title('未命名文件')
filename=None
textPad.delete(1.0,END)
def save():
global filename
#filename=r'c:\node.txt'
try:
f=open(filename,'w')
msg=textPad.get(1.0,END)
f.write(msg)
f.close()
except:
saveas()
def saveas():
f=asksaveasfilename(initialfile='未命名.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))
def cut():
textPad.event_generate('<>')
def copy():
textPad.event_generate('<>')
def paste():
textPad.event_generate('<>')
def redo():
textPad.event_generate('<>')
def undo():
textPad.event_generate('<>')
def selectAll():
textPad.tag_add('sel','1.0',END)
def search():
v=StringVar()
def genSerach():
s=v.get()
start = 1.0
while 1:
pos=textPad.search(s, start, stopindex=END)
if not pos:
break
textPad.tag_add('sel',pos)
textPad.focus_set()
start=pos + "+1c"
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,textvariable=v)
entry1.grid(row=0, column=1,padx=5)
button1 = Button(topsearch,text='查找',command=genSerach)
button1.grid(row=0, column=2)
root=Tk()
root.title('Linsy Node')
root.geometry('800x500+100+100')
menubar=Menu(root)
root.config(menu=menubar)
filemenu=Menu(menubar)
filemenu.add_command(label='新建',accelerator='Ctrl+N',command=new)
filemenu.add_command(label='打开',accelerator='Ctrl+O',command=opens)
filemenu.add_command(label='保存',accelerator='Ctrl+S',command=save)
filemenu.add_command(label='另存为',accelerator='Ctrl+Shift+S',command=saveas)
menubar.add_cascade(label='文件',menu=filemenu)
editmenu=Menu(menubar)
editmenu.add_command(label='撤销',accelerator='Ctrl+x',command=undo)
editmenu.add_command(label='重做',accelerator='Ctrl+y',command=redo)
editmenu.add_separator()
editmenu.add_command(label='剪切',accelerator='Ctrl+x',command=cut)
editmenu.add_command(label='复制',accelerator='Ctrl+s',command=copy)
editmenu.add_command(label='粘贴',accelerator='Ctrl+v',command=paste)
editmenu.add_separator()
editmenu.add_command(label='查找',accelerator='Ctrl+f',command=search)
editmenu.add_command(label='全选',accelerator='Ctrl+a',command=selectAll)
menubar.add_cascade(label='编辑',menu=editmenu)
aboutmenu=Menu(menubar)
aboutmenu.add_command(label='作者',command=author)
aboutmenu.add_command(label='版权',command=about)
menubar.add_cascade(label='关于',menu=aboutmenu)
toolbar=Frame(root,height=250,bg='light sea green')
shortButton=Button(toolbar,text='打开',command=opens)
shortButton.pack(side=LEFT,padx=5,pady=5)
shortButton=Button(toolbar,text='保存',command=save)
shortButton.pack(side=LEFT)
toolbar.pack(fill=X)
status=Label(root,text='Ln20',bd=1,relief=SUNKEN,anchor=W)
status.pack(side=BOTTOM,fill=X)
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)
root.mainloop() |