python 實(shí)現(xiàn)簡易的記事本
運(yùn)行效果
完整代碼
from tkinter import * from tkinter.filedialog import * from tkinter.messagebox import * import os filename='' def author(): showinfo('大道至簡','簡易記事本第一版') def power(): showinfo('版權(quán)信息','本公司保留版權(quán)信息,不可以把本軟件用于商業(yè)目的!') def myopen(): 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 root,filename,textPad 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=asksaveasfile(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('簡易記事本'+os.path.basename(f)) def cut(): global textPad textPad.event_generate('<<Cut>>') def copy(): global textPad textPad.event_generate('<<Copy>>') def paste(): global textPad textPad.event_generate('<<Paste>>') def undo(): global textPad textPad.event_generate('<<Undo>>') def redo(): global textPad textPad.event_generate('<<Redo>>') def select_all(): global textPad textPad.tag_add('sel','1.0','end') def find(): global root 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='不區(qū)分大小寫',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) def close_search(): textPad.tag_remove('match','1.0',END) t.destroy() t.protocol('WM_DELETE_WINDOW',close_search)#??? def search(needle,cssnstv,textPad,t,e): textPad.tag_remove('match','1.0',END) count=0 if needle: pos='1.0' while True: pos=textPad.search(needle,pos,nocase=cssnstv,stopindex=END) if not pos:break lastpos=pos+str(len(needle)) textPad.tag_add('match',pos,lastpos) count+=1 pos=lastpos textPad.tag_config('match',foreground='yellow',background='green') e.focus_set() t.title(str(count)+'個被匹配') def popup(event): global editmenu 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)#制作菜單實(shí)例,依附于父窗口root上面 filemenu=Menu(menubar)#制作文件菜單項(xiàng),依附于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)#制作編輯菜單項(xiàng),依附于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='復(fù)制',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)#制作關(guān)于菜單項(xiàng),依附于menubar菜單上面 menubar.add_cascade(label='關(guān)于',menu=aboutmenu)#增加分層菜單 aboutmenu.add_command(label='作者',command=author) aboutmenu.add_command(label='版權(quán)',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('<Control-N>',new) textPad.bind('<Control-n>',new) textPad.bind('<Control-O>',myopen) textPad.bind('<Control-o>',myopen) textPad.bind('<Control-S>',save) textPad.bind('<Control-s>',save) textPad.bind('<Control-A>',select_all) textPad.bind('<Control-a>',select_all) textPad.bind('<Control-f>',find) textPad.bind('<Control-F>',find) textPad.bind('<Control-3>',popup) root.mainloop()
以上就是python 實(shí)現(xiàn)簡易的記事本的詳細(xì)內(nèi)容,更多關(guān)于python 實(shí)現(xiàn)記事本的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
巧妙使用Python裝飾器處理if...elif...else
大家好,今天在 Github 閱讀 EdgeDB[1] 的代碼,發(fā)現(xiàn)它在處理大量if…elif…else的時候,巧妙地使用了裝飾器,方法設(shè)計(jì)精巧,分享給大家一下,歡迎收藏學(xué)習(xí),喜歡點(diǎn)贊支持2021-11-11Python中執(zhí)行調(diào)用JS的多種實(shí)現(xiàn)方法總結(jié)
這篇文章主要給大家介紹了關(guān)于Python中執(zhí)行調(diào)用JS的多種實(shí)現(xiàn)方法,在一些特殊的python應(yīng)用場景下需要逆向執(zhí)行javascript代碼塊或者.js文件,需要的朋友可以參考下2023-08-08python繪制y關(guān)于x的線性回歸線性方程圖像實(shí)例
這篇文章主要為大家介紹了python繪制y關(guān)于x的線性回歸線性方程圖像實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10如何在python中用os模塊實(shí)現(xiàn)批量移動文件
在工作中難免會遇到需要批量整理文件的情況,當(dāng)需要從一堆文件中將部分文件批量地轉(zhuǎn)移時,如果手工一一轉(zhuǎn)移難免浪費(fèi)時間,這篇文章主要給大家介紹了關(guān)于如何在python中用os模塊實(shí)現(xiàn)批量移動文件的相關(guān)資料,需要的朋友可以參考下2022-05-05Python實(shí)現(xiàn)智能貪吃蛇游戲的示例代碼
我想大家都玩過諾基亞上面的貪吃蛇吧,這篇文章將帶你一步步用python語言實(shí)現(xiàn)一個snake小游戲,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-07-07pytorch載入預(yù)訓(xùn)練模型后,實(shí)現(xiàn)訓(xùn)練指定層
今天小編就為大家分享一篇pytorch載入預(yù)訓(xùn)練模型后,實(shí)現(xiàn)訓(xùn)練指定層,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01淺談python for循環(huán)的巧妙運(yùn)用(迭代、列表生成式)
下面小編就為大家?guī)硪黄獪\談python for循環(huán)的巧妙運(yùn)用(迭代、列表生成式)。2017-09-09