用python编写最简单的记事本_python做简易记事本

from tkinter import *

from tkinter.filedialog import *

from tkinter.messagebox import *

importos

filename=''

defauthor():

showinfo('大道至简','简易记事本第一版')defpower():

showinfo('版权信息','本公司保留版权信息,不可以把本软件用于商业目的!')defmyopen():globalfilename

filename=askopenfilename(defaultextension='.txt')if filename=='':

filename=Noneelse:

root.title('简易记事本'+os.path.basename(filename))

textPad.delete(1.0,END)

f=open(filename,'r')

textPad.insert(1.0,f.read())

f.close()defnew():globalroot,filename,textPad

root.title('未命名文件')

filename=None

textPad.delete(1.0,END)defsave():globalfilenametry:

f=open(filename,'w')

msg=textPad.get(1.0,'end')

f.write(msg)

f.close()except:

saveas()defsaveas():

f=asksaveasfile(initialfile='未命名.txt',defaultextension='.txt')globalfilename

filename=f

fh=open(f,'w')

msg=textPad.get(1.0,END)

fh.write(msg)

fh.close()

root.title('简易记事本'+os.path.basename(f))defcut():globaltextPad

textPad.event_generate('<>')defcopy():globaltextPad

textPad.event_generate('<>')defpaste():globaltextPad

textPad.event_generate('<>')defundo():globaltextPad

textPad.event_generate('<>')defredo():globaltextPad

textPad.event_generate('<>')defselect_all():globaltextPad

textPad.tag_add('sel','1.0','end')deffind():globalroot

t=Toplevel(root)

t.title('查找')

t.geometry('260x60+200+250')

t.transient(root)

Label(t,text='查找:').grid(row=0,column=0,sticky='e')

v=StringVar()

e=Entry(t,width=20,textvariable=v)

e.grid(row=0,column=1,padx=2,pady=2,sticky='we')

e.focus_set()

c=IntVar()

Checkbutton(t,text='不区分大小写',variabel=c).grid(row=1,column=1,sticky='e')

Button(t,text='查找所有',command=lambda :search(v.get(),c.get(),textPad,t,e)).grid(row=0,

column=2,sticky='e'+'w',padx=2,pady=2)defclose_search():

textPad.tag_remove('match','1.0',END)

t.destroy()

t.protocol('WM_DELETE_WINDOW',close_search)#???

defsearch(needle,cssnstv,textPad,t,e):

textPad.tag_remove('match','1.0',END)

count=0ifneedle:

pos='1.0'

whileTrue:

pos=textPad.search(needle,pos,nocase=cssnstv,stopindex=END)if not pos:breaklastpos=pos+str(len(needle))

textPad.tag_add('match',pos,lastpos)

count+=1pos=lastpos

textPad.tag_config('match',foreground='yellow',background='green')

e.focus_set()

t.title(str(count)+'个被匹配')defpopup(event):globaleditmenu

editmenu.tk_popup(event.x_root,event.y_root)

root=Tk()

root.title('简易记事本第一版')

root.geometry('300x300+100+100')#geometry(wxh+xoffset+yoffset)

menubar=Menu(root)#制作菜单实例,依附于父窗口root上面

filemenu=Menu(menubar)#制作文件菜单项,依附于menubar菜单上面

menubar.add_cascade(label='文件',menu=filemenu)#增加分层菜单

filemenu.add_command(label='新建',accelerator='Ctrl+N',command=new)

filemenu.add_command(label='打开',accelerator='Ctrl+O',command=myopen)

filemenu.add_command(label='保存',accelerator='Ctrl+S',command=save)

filemenu.add_command(label='另存为',accelerator='Ctrl+Alt+S',command=saveas)

editmenu=Menu(menubar)#制作编辑菜单项,依附于menubar菜单上面

menubar.add_cascade(label='编辑',menu=editmenu)

editmenu.add_command(label='撤销',accelerator='Ctrl+Z',command=undo)

editmenu.add_command(label='重做',accelerator='Ctrl+Y',command=redo)

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',command=find)

editmenu.add_command(label='全选',accelerator='Ctrl+A',command=select_all)

aboutmenu=Menu(menubar)#制作关于菜单项,依附于menubar菜单上面

menubar.add_cascade(label='关于',menu=aboutmenu)#增加分层菜单

aboutmenu.add_command(label='作者',command=author)

aboutmenu.add_command(label='版权',command=power)

root.config(menu=menubar)

shortcutbar=Frame(root,height=25,bg='light sea green')

shortcutbar.pack(expand=NO,fill=X)

Inlabel=Label(root,width=2,bg='antique white')

Inlabel.pack(side=LEFT,anchor='nw',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)

textPad.bind('',new)

textPad.bind('',new)

textPad.bind('',myopen)

textPad.bind('',myopen)

textPad.bind('',save)

textPad.bind('',save)

textPad.bind('',select_all)

textPad.bind('',select_all)

textPad.bind('',find)

textPad.bind('',find)

textPad.bind('',popup)

root.mainloop()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一个使用Python编写简易记事本,以下是代码: ```python from tkinter import * from tkinter.filedialog import askopenfilename, asksaveasfilename # 创建窗口 root = Tk() root.title("记事本") root.geometry("600x600") # 创建文本框 text_area = Text(root, font=("Helvetica", 12)) text_area.pack(expand=True, fill=BOTH) # 新建文件 def new_file(): root.title("未命名文件") text_area.delete("1.0", END) # 打开文件 def open_file(): filepath = askopenfilename(defaultextension=".txt", filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")]) if not filepath: return text_area.delete("1.0", END) with open(filepath, "r") as input_file: text = input_file.read() text_area.insert("1.0", text) root.title(f"{filepath}") # 保存文件 def save_file(): filepath = asksaveasfilename(defaultextension=".txt", filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")]) if not filepath: return with open(filepath, "w") as output_file: text = text_area.get("1.0", END) output_file.write(text) root.title(f"{filepath}") # 菜单栏 menu_bar = Menu(root) # 文件菜单 file_menu = Menu(menu_bar, tearoff=0) file_menu.add_command(label="新建", command=new_file) file_menu.add_command(label="打开", command=open_file) file_menu.add_command(label="保存", command=save_file) file_menu.add_separator() file_menu.add_command(label="退出", command=root.quit) menu_bar.add_cascade(label="文件", menu=file_menu) # 添加菜单栏 root.config(menu=menu_bar) root.mainloop() ``` 您可以复制上述代码保存为 Python 文件,然后运行即可使用。希望能对您有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值