Python开发的简单记事本

---恢复内容开始---

                主要是利用python 自带的tkinter 库

  

程序的基于python3.0以上 ,各个平台都可以使用包括linux ,windows ,OSX,

 

 

代码是:

#!/usr/bin/python
from  tkinter   import  *
from  tkinter.messagebox  import  *
from tkinter.filedialog  import *
import os
filename=''
def author():
    showinfo("作者信息",'本软件由yubenliu完成')
def about():
    showinfo('软件归属','本软件版权归属于yubenliu')
def ybl():
    global  filename
    filename=askopenfilename(defaultextension='.txt')
    if filename =='':
        filename=None
    else:
        root.title(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
    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()
    root.title(os.path.basename(f))
def cut():
    textpad.event_generate('<<Cut>>')
def copy():
    textpad.event_generate('<<Copy>>')
def undo():
    textpad.event_generate('<<Undo>>')
def redo():
    textpad.event_generate('<<Redo>>')
def paste():
    textpad.event_generate('<<Paste>>')
def selectall():
    textpad.tag_add('sel','1.0',END)

root= Tk()
root.title('yubenliu')
root.geometry('500x500+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=ybl)
filemenu.add_command(label='保存',accelerator='ctrl+ S',command=save)
filemenu.add_command(label='另存为',accelerator='ctrl+W',command=saveas)
menubar.add_cascade(label='文件',menu=filemenu)
editmenu=Menu(menubar)
#编辑的创建
editmenu.add_command(label='撤销',accelerator='ctrl + Z',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 + c',command=copy)
editmenu.add_command(label='粘贴',accelerator='ctrl + v',command=paste)
editmenu.add_separator()
editmenu.add_command(label='查找',accelerator='ctrl + F')
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)
aboutmenu.add_command(label='关于')
menubar.add_cascade(label='关于',menu=aboutmenu)
#工具栏
toobar=Frame(root,height=25,bg='light sea green')
shortButton=Button(toobar,text='打开',command=ybl)
shortButton.pack(side=LEFT,padx=5,pady=5)
shortButton=Button(toobar,text='保存',command=save)
shortButton.pack(side=LEFT)
toobar.pack(fill=X,expand=NO)
#状态栏
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()

  

 程序执行的界面:

 

  

 

posted @ 2016-08-13 18:17  下丶雨天  阅读(870)  评论(0编辑  收藏  举报