python tkinter文本编辑器_【转】tkinter实现的文本编辑器

1 #-*- encoding: utf8 -*-

2 #python 2.7

3 from Tkinter import *

4 from tkMessageBox import *

5 from tkFileDialog import *

6 importos7

8

9 filename = ''

10

11 defauthor():12 showinfo('author:','sundy')13

14 defabout():15 showinfo('Copyright:','sundy')16

17 defopenfile():18 globalfilename19 filename = askopenfilename(defaultextension = '.txt')20 if filename == '':21 filename =None22 else:23 root.title('FileName:'+os.path.basename(filename))24 textPad.delete(1.0,END)25 f = open(filename,'r')26 textPad.insert(1.0,f.read())27 f.close()28

29 defnew():30 globalfilename31 root.title('未命名文件')32 filename =None33 textPad.delete(1.0,END)34

35 defsave():36 globalfilename37 try:38 f = open(filename,'w')39 msg = textPad.get(1.0,END)40 f.write(msg)41 f.close()42 except:43 saveas()44

45

46 defsaveas():47 f = asksaveasfilename(initialfile= '未命名.txt', defaultextension='.txt')48 globalfilename49 filename =f50 fh = open(f,'w')51 msg = textPad.get(1.0,END)52 fh.write(msg)53 fh.close()54 root.title('FileName:'+os.path.basename(f))55

56 defcut():57 textPad.event_generate('<>')58

59 defcopy():60 textPad.event_generate('<>')61

62 defpaste():63 textPad.event_generate('<>')64

65 defredo():66 textPad.event_generate('<>')67

68 defundo():69 textPad.event_generate('<>')70

71 defselectAll():72 textPad.tag_add('sel','1.0',END)73

74 defsearch():75 defdosearch():76 myentry = entry1.get() #获取查找的内容--string型

77 whatever = str(textPad.get(1.0,END))78 #print textPad.index('zxc')

79 #print myentry

80 #print "%d个"%(whatever.count(myentry)) #计算substr在S中出现的次数

81 showinfo("查找结果:","you searched %s, there are %d in the text"%(myentry,whatever.count(myentry)))82 #print whatever.find(myentry)

83

84 #teIndex = textPad.index(myentry)

85 #textPad.linestart(teIndex)

86 #textPad.mark_set('insert', teIndex)

87 #textPad.mark_set(myentry,CURRENT + '+5c')

88 #textPad.mark_set(myentry,CURRENT + ' wordstart')

89 topsearch =Toplevel(root)90 topsearch.geometry('300x30+200+250')91 label1 = Label(topsearch,text='Find')92 label1.grid(row=0, column=0,padx=5)93 entry1 = Entry(topsearch,width=20)94 entry1.grid(row=0, column=1,padx=5)95 button1 = Button(topsearch,text='查找',command=dosearch)96 button1.grid(row=0, column=2)97

98

99 root =Tk()100 root.title('Sundy Node')101 root.geometry("800x500+100+100")102

103 #Create Menu

104 menubar =Menu(root)105 root.config(menu =menubar)106

107 filemenu =Menu(menubar)108 filemenu.add_command(label='新建', accelerator='Ctrl + N', command=new)109 filemenu.add_command(label='打开', accelerator='Ctrl + O',command =openfile)110 filemenu.add_command(label='保存', accelerator='Ctrl + S', command=save)111 filemenu.add_command(label='另存为', accelerator='Ctrl + Shift + S',command=saveas)112 menubar.add_cascade(label='文件',menu=filemenu)113

114 editmenu =Menu(menubar)115 editmenu.add_command(label='撤销', accelerator='Ctrl + Z', command=undo)116 editmenu.add_command(label='重做', accelerator='Ctrl + y', command=redo)117 editmenu.add_separator()118 editmenu.add_command(label = "剪切",accelerator = "Ctrl + X",command=cut)119 editmenu.add_command(label = "复制",accelerator = "Ctrl + C", command=copy)120 editmenu.add_command(label = "粘贴",accelerator = "Ctrl + V", command=paste)121 editmenu.add_separator()122 editmenu.add_command(label = "查找",accelerator = "Ctrl + F", command=search)123 editmenu.add_command(label = "全选",accelerator = "Ctrl + A", command=selectAll)124 menubar.add_cascade(label = "操作",menu =editmenu)125 aboutmenu =Menu(menubar)126 aboutmenu.add_command(label = "作者", command=author)127 aboutmenu.add_command(label = "关于", command =about)128 menubar.add_cascade(label = "about",menu=aboutmenu)129

130 #toolbar

131 toolbar = Frame(root, height=25,bg='grey')132 shortButton = Button(toolbar, text='打开',command =openfile)133 shortButton.pack(side=LEFT, padx=5, pady=5)134

135 shortButton = Button(toolbar, text='保存', command =save)136 shortButton.pack(side=LEFT)137 toolbar.pack(expand=NO,fill=X)138

139 #Status Bar

140 status = Label(root, text='Ln20',bd=1, relief=SUNKEN,anchor=W)141 status.pack(side=BOTTOM, fill=X)142

143 #linenumber&text

144 lnlabel =Label(root, width=2, bg='antique white')145 lnlabel.pack(side=LEFT, fill=Y)146

147 textPad = Text(root, undo=True)148 textPad.pack(expand=YES, fill=BOTH)149

150 scroll =Scrollbar(textPad)151 textPad.config(yscrollcommand=scroll.set)152 scroll.config(command =textPad.yview)153 scroll.pack(side=RIGHT,fill=Y)154

155 root.mainloop()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值