用python编写最简单的记事本_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('<>')

def copy():

textpad.event_generate('<>')

def undo():

textpad.event_generate('<>')

def redo():

textpad.event_generate('<>')

def paste():

textpad.event_generate('<>')

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()

程序执行的界面:

955641-20160813181702562-1255714095.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值