python 实现简易的记事本

作者:gisoracle 时间:2022-10-17 18:24:21 

运行效果

python 实现简易的记事本

完整代码


from tkinter import *
from tkinter.filedialog import *
from tkinter.messagebox import *
import os
filename=''
def author():
 showinfo('大道至简','简易记事本第一版')

def power():
 showinfo('版权信息','本公司保留版权信息,不可以把本软件用于商业目的!')
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='不区分大小写',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)#制作菜单实例,依附于父窗口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('<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()

来源:https://www.cnblogs.com/gisoracle/p/12025076.html

标签:python,记事本
0
投稿

猜你喜欢

  • 不用为美化select烦恼模仿combox(select)控件

    2007-08-04 21:08:00
  • Jupyter Notebook添加代码自动补全功能的实现

    2021-01-07 07:31:12
  • 浅述python2与python3的简单区别

    2022-03-06 21:19:47
  • windows下python安装pip方法详解

    2023-12-13 19:43:16
  • python获得一个月有多少天的方法

    2022-02-21 00:55:15
  • python判断字符串的前两个字母是否是"id"的示例代码

    2021-05-02 20:39:12
  • php封装json通信接口详解及实例

    2023-11-14 21:56:26
  • 用python批量移动文件

    2022-12-21 10:48:43
  • Python中的groupby分组功能的实例代码

    2021-09-17 20:48:15
  • 使用Python为中秋节绘制一块美味的月饼

    2023-06-30 17:36:10
  • pyspark给dataframe增加新的一列的实现示例

    2022-06-13 20:00:19
  • 在系统崩溃的时候如何恢复原有的数据

    2009-01-08 13:26:00
  • python os模块简单应用示例

    2021-12-20 13:36:48
  • win32com操作word之Application&Documents接口学习

    2021-03-01 13:30:34
  • Python实现自动登录百度空间的方法

    2023-11-11 09:11:23
  • Python中一般处理中文的几种方法

    2023-10-11 01:45:23
  • SQL语句执行顺序图文介绍

    2023-07-04 22:59:27
  • 安装了Office2003补丁之后,access不能用,打不开了

    2011-05-12 12:19:00
  • Windows环境下python环境安装使用图文教程

    2023-12-25 10:13:26
  • 人脸检测实战终极之OpenCV+Python实现人脸对齐

    2023-10-01 02:03:07
  • asp之家 网络编程 m.aspxhome.com