标签:
此代码是看完如下教学视频实现的,所以算是【转载】吧:
效果:
代码:
1 # -*- encoding: utf8 -*- 2 #python 2.7 3 from Tkinter import * 4 from tkMessageBox import * 5 from tkFileDialog import * 6 import os 7 8 9 filename = ‘‘ 10 11 def author(): 12 showinfo(‘author:‘,‘sundy‘) 13 14 def about(): 15 showinfo(‘Copyright:‘,‘sundy‘) 16 17 def openfile(): 18 global filename 19 filename = askopenfilename(defaultextension = ‘.txt‘) 20 if filename == ‘‘: 21 filename = None 22 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 def new(): 30 global filename 31 root.title(‘未命名文件‘) 32 filename = None 33 textPad.delete(1.0,END) 34 35 def save(): 36 global filename 37 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 def saveas(): 47 f = asksaveasfilename(initialfile= ‘未命名.txt‘, defaultextension=‘.txt‘) 48 global filename 49 filename = f 50 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 def cut(): 57 textPad.event_generate(‘<<Cut>>‘) 58 59 def copy(): 60 textPad.event_generate(‘<<Copy>>‘) 61 62 def paste(): 63 textPad.event_generate(‘<<Paste>>‘) 64 65 def redo(): 66 textPad.event_generate(‘<<Redo>>‘) 67 68 def undo(): 69 textPad.event_generate(‘<<Undo>>‘) 70 71 def selectAll(): 72 textPad.tag_add(‘sel‘,‘1.0‘,END) 73 74 def search(): 75 def dosearch(): 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()
推荐文档:http://effbot.org/tkinterbook/
*python2.7环境。。在python3.X中tkinter有改变,请注意!!
标签:
原文地址:http://www.cnblogs.com/pengsixiong/p/4944963.html